-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
42 lines (33 loc) · 1.04 KB
/
server.py
File metadata and controls
42 lines (33 loc) · 1.04 KB
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
import socket
from utils import log
import threading
from request import Request
from routes import router
def handle_request(connection, address):
received_bytes = connection.recv(4096)
http_text = received_bytes.decode('utf-8')
if len(http_text.split()) < 2:
connection.close()
return
request = Request.from_http_and_address(http_text, address)
log('Request is {}'.format(request))
log('Request path is {}'.format(request.path))
response = router.handle(request)
# log('Response:\n' + response.decode('utf-8'))
log('response is', response)
connection.sendall(response)
connection.close()
def run(host, port):
log('start at {}{}'.format(host, port))
with socket.socket() as s:
s.bind((host, port))
s.listen(5)
while True:
connection, address = s.accept()
threading.Thread(target=handle_request, args=(connection, address)).start()
if __name__ == '__main__':
config = {
'host': '',
'port': 8000,
}
run(**config)