-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
104 lines (72 loc) · 2.39 KB
/
Copy pathserver.py
File metadata and controls
104 lines (72 loc) · 2.39 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import socket
from router import route
from request_parser import HTTPRequest
import socket
import re
HOST = '127.0.0.1'
PORT = 8080
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(1)
print(f"server running on http://{HOST}:{PORT}")
def _read_until_content_length(sock) -> bytes:
"""Extract body from POST request, Parse out the content-length value"""
# read the header completly until the \r\n\r\n boundary
header_buffer = bytearray()
while b"\r\n\r\n" not in header_buffer:
chunk = sock.recv(4096)
if not chunk:
raise ConnectionError("Socket closed while reading headers.")
header_buffer.extend(chunk)
if not header_buffer:
return ""
# seperate the header section from any early body data
header_bytes, body_buffer = header_buffer.split(b"\r\n\r\n", 1)
header_text = header_bytes.decode()
content_length = 0
for line in header_text.split("\r\n"):
if line.lower().startswith("content-length:"):
content_length = int(line.split(":", 1)[1].strip())
break
while len(body) < content_length:
chunk = sock.recv(4096)
if not chunk:
break
body.extend(chunk)
raw_request = (
header_bytes
+ b"\r\n\r\n"
+ body
)
return raw_request.decode(errors="ignore")
while True:
client_socket, client_address = server_socket.accept()
raw_request = _read_until_content_length(client_socket)
# print("RAW REQUEST:")
# print(repr(request))
if not raw_request:
client_socket.close()
continue
# print(raw_request)
# parse request
request = HTTPRequest(raw_request)
# request_line = request.split('\n')[0]
# method, path, version = request_line.split()
method = request.method
path = request.path
headers = request.headers
# parts = request.split("\r\n\r\n", 1)
# body = ""
# if len(parts) > 1:
# body = parts[1].strip()
status, content_type, response_body = route(method, path, body)
print("Method: ", method)
print("Path: ", path)
print("Body=", repr(body))
response = (
f"HTTP/1.1 {status}\n"
f"Content-Type: {content_type}\n\n"
f"{response_body}"
)
client_socket.send(response.encode())
client_socket.close()