-
Notifications
You must be signed in to change notification settings - Fork 1
/
HttpServer.py
64 lines (54 loc) · 2.25 KB
/
HttpServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
""" Sudoku HTTP Server"""
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import time
from sudoku import Sudoku
class sudokuHTTP(BaseHTTPRequestHandler):
def __init__(self, callback, *args, **kwargs):
self.callback = callback
super().__init__(*args, **kwargs)
def do_POST(self):
if self.path == '/solve':
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
sudoku = json.loads(post_data)
# print(sudoku)
# self.callback(sudoku) # chamar o callback
# receive what comes from the callback
solved_sudoku = self.callback(sudoku)
# build a json response
response = {'sudoku': solved_sudoku}
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(response).encode())
else:
self.send_response(404)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({'error': 'Page not found! Go to /solve istead'}).encode())
def do_GET(self):
if self.path == '/stats':
# retornar os status da rede p2p
status = self.callback('stats')
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(status).encode())
elif self.path == '/network':
# retornar a lista de connexões
network = self.callback('network')
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(network).encode())
else:
self.send_response(404)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps({'error': 'Page not found, check /stats or /network'}).encode())
# server = HTTPServer(('localhost', 8080), sudokuHTTP)
# print('Server running ...')
# server.serve_forever()
# server.server_close()
# print('Server stopped.')