-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
52 lines (47 loc) · 2.02 KB
/
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
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
import os
import responses
(default_response, debug_response) = responses.responses()
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/data':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(default_response).encode())
elif self.path == '/debug_data':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(debug_response).encode())
elif self.path == '/' or self.path == '/index.html':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
with open('client/index.html', 'rb') as file:
self.wfile.write(file.read())
elif self.path.startswith('/scripts/') and self.path.endswith('.mjs'):
# Serve arbitrary JavaScript files from the scripts folder
file_path = os.path.join('client', self.path.lstrip('/'))
if os.path.isfile(file_path):
self.send_response(200)
self.send_header('Content-type', 'text/javascript')
self.end_headers()
with open(file_path, 'rb') as file:
self.wfile.write(file.read())
else:
self.send_response(404)
self.end_headers()
self.wfile.write(b'{"error": "File not found"}')
else:
self.send_response(404)
self.end_headers()
self.wfile.write(b'{"error": "Not found"}')
def run(handler_class=SimpleHTTPRequestHandler, port=8000):
server_address = ('', port)
httpd = HTTPServer(server_address, handler_class)
print(f'Starting ship http server on port {port}')
httpd.serve_forever()
if __name__ == '__main__':
run()