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
@@ -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
4656def 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
6899def main ():
69100 server_socket = socket (AF_INET , SOCK_STREAM )
0 commit comments