forked from PhoenixHe-NV/network-pj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-server.py
67 lines (51 loc) · 1.76 KB
/
test-server.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
65
66
67
import socketserver
class TestTCPServer(socketserver.TCPServer):
def __init__(self, server_port, request_handler_class):
self.allow_reuse_address = True
super().__init__(('0.0.0.0', server_port), request_handler_class)
self.serve_forever()
class SimpleTestHTTPHandler(socketserver.BaseRequestHandler):
def __init__(self, request, client_address, server, name):
super().__init__(request, client_address, server)
self.path = ""
self.name = name
def finish(self):
print(self.name, self.request.raddr, self.path)
self.request.close()
def response_404(self):
self.request.sendall(
b'''404 Not Found HTTP/1.1\r\nConnection: Close\r\n\r\n'''
)
def handle(self):
self.path = ""
head = self.request.recv(1024)
space_p = head.find(b' ')
if space_p < 0:
self.response_404()
return
head = head[space_p + 1:]
space_p = head.find(b' ')
if space_p < 0:
self.response_404()
return
self.path = head[: space_p]
self.handle_route(self.path)
def handle_route(self, path):
pass
class BrokenRspHandler(SimpleTestHTTPHandler):
def __init__(self, request, client_address, server):
super().__init__(request, client_address, server, "Broken Response")
def handle_route(self, path):
head200 = b'200 OK HTTP/1.1\r\n'
rsp = {
b'/test/0': b'200 OK HTTP1.1\r\n',
b'/test/1': b'200 OK HTTP1.1\r\n',
}.get(path)
if rsp is None:
self.response_404()
return
self.request.sendall(rsp)
def main():
TestTCPServer(8031, BrokenRspHandler)
if __name__ == '__main__':
main()