-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvas-server.py
62 lines (48 loc) · 1.76 KB
/
vas-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
#!/usr/bin/env python
from http.server import BaseHTTPRequestHandler
from socketserver import ThreadingTCPServer
import json
import time
ADDRESS = "localhost"
PORT = 1338
class VASServer(ThreadingTCPServer):
def __init__(self, *args, **kwargs):
ThreadingTCPServer.__init__(self, *args, **kwargs)
self.VASResponses = {}
class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers["Content-Length"])
body = self.rfile.read(content_length).decode()
thread = json.loads(body)["~thread"]
if thread:
thid = thread["thid"]
print("Received POST request. Body: " + body)
self.server.VASResponses[thid] = body
def do_GET(self):
self.send_response(200)
self.end_headers()
print("Received GET request. Path: " + self.path)
if self.path == '/':
response = json.dumps(self.server.VASResponses)
elif self.path.startswith("/"):
thid = self.path[1:]
for i in range(1, 10, 1):
temp = self.server.VASResponses[thid]
if temp is not None:
response = temp
break
elif i == 10:
response = '{}'
print("No response on " + i + "th iteration, waiting for 10s before retry.")
time.sleep(10)
else:
response = '{}'
print("Received GET request. Response: " + response)
self.wfile.write(response.encode())
def run():
server_address = (ADDRESS, PORT)
server = VASServer(server_address, RequestHandler)
print(f"Starting VAS HTTP server on {ADDRESS}:{PORT}")
server.serve_forever()
if __name__ == "__main__":
run()