11from utils import http_parser_request
2+ from utils import readjson
3+ from utils import writejson
24
35from socket import socket , AF_INET , SOCK_STREAM
46from 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
2636def 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
0 commit comments