Skip to content

Commit 10fab48

Browse files
committed
Merge branch 'branch-gio'
2 parents d853475 + cff5bbe commit 10fab48

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

data/keys.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
[]
1+
[
2+
{"chave": 1234},
3+
{"chave": 4567}
4+
]

src/main_server.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from utils import http_parser_request
2+
from utils import readjson
3+
from utils import writejson
24

35
from socket import socket, AF_INET, SOCK_STREAM
46
from threading import Thread
@@ -40,8 +42,16 @@ def http_get(socket_client, request):
4042

4143
socket_client.send(msgHtml.encode())
4244

43-
def http_post():
44-
...
45+
def http_post(socket_client):
46+
#duvida de como seria o reply
47+
#client ainda não votou
48+
msgHeader = 'HTTP/1.1 200 OK \r\n' \
49+
'Host: voting.com\r\n' \
50+
'Content-Type: text/html\r\n' \
51+
'\r\n'
52+
#'voto': registrado
53+
54+
socket_client.send(msgHeader.encode())
4555

4656
def handle_request(socket_client):
4757

@@ -64,6 +74,27 @@ def handle_request(socket_client):
6474
elif(request['Method'] == 'POST'):
6575
http_post()
6676

77+
elif(request['Method'] == 'POST'):
78+
votou = False
79+
#adicionar o voto
80+
for cliente in readjson("keys"):
81+
if cliente['chave'] == request['Key']:
82+
#caso a pessoa já votou
83+
votou = True
84+
#reply de já votou http_post()
85+
86+
87+
#caso a pessoa ainda não votou
88+
if votou == False:
89+
#adiciona a chave no keys.json
90+
keys_dados = readjson("keys")
91+
keys_dados.append({"chave": request['Key']})
92+
writejson("keys", keys_dados)
93+
94+
#responder o voto registrado
95+
http_post(socket_client)
96+
97+
return
6798

6899
def main():
69100
server_socket = socket(AF_INET, SOCK_STREAM)

src/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
import os
12
import json
23

34
def readjson(path):
4-
f = open(path, 'r')
5+
file = os.path.join("./data", f"{path}.json")
6+
f = open(file, 'r')
57
data = json.load(f)
68
f.close()
79
return data
810

11+
def writejson(data, path):
12+
file = os.path.join("./data", f"{path}.json")
13+
f = open(file, 'w')
14+
json.dump(data, f)
15+
f.close()
16+
917
def http_parser_request(html_string):
1018

1119
map = dict()

0 commit comments

Comments
 (0)