Skip to content

Commit cff5bbe

Browse files
committed
mudanças no método post do handle_request
1 parent b98bb0d commit cff5bbe

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
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: 30 additions & 3 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
@@ -20,8 +22,16 @@ def http_get(socket_client):
2022

2123
socket_client.send(msgHtml.encode())
2224

23-
def http_post():
24-
...
25+
def http_post(socket_client):
26+
#duvida de como seria o reply
27+
#client ainda não votou
28+
msgHeader = 'HTTP/1.1 200 OK \r\n' \
29+
'Host: voting.com\r\n' \
30+
'Content-Type: text/html\r\n' \
31+
'\r\n'
32+
#'voto': registrado
33+
34+
socket_client.send(msgHeader.encode())
2535

2636
def handle_request(socket_client):
2737
req = socket_client.recv(2048).decode()
@@ -35,7 +45,24 @@ def handle_request(socket_client):
3545
http_get(socket_client)
3646

3747
elif(request['Method'] == 'POST'):
38-
http_post()
48+
votou = False
49+
#adicionar o voto
50+
for cliente in readjson("keys"):
51+
if cliente['chave'] == request['Key']:
52+
#caso a pessoa já votou
53+
votou = True
54+
#reply de já votou http_post()
55+
56+
57+
#caso a pessoa ainda não votou
58+
if votou == False:
59+
#adiciona a chave no keys.json
60+
keys_dados = readjson("keys")
61+
keys_dados.append({"chave": request['Key']})
62+
writejson("keys", keys_dados)
63+
64+
#responder o voto registrado
65+
http_post(socket_client)
3966

4067
return
4168

src/utils.py

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

3-
def readjson():
4-
f = open('./data/dns.json', 'r')
4+
def readjson(path):
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)