-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
30 lines (25 loc) · 933 Bytes
/
server.py
File metadata and controls
30 lines (25 loc) · 933 Bytes
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
import http.server
import socketserver
import sys
PORT = 8000
class Handler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
self.send_header('Access-Control-Allow-Origin', '*')
super().end_headers()
def guess_type(self, path):
mimetype = super().guess_type(path)
if path.endswith(".js"):
return "application/javascript"
return mimetype
# Allow port reuse
socketserver.TCPServer.allow_reuse_address = True
try:
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"Serving at http://localhost:{PORT}")
httpd.serve_forever()
except OSError:
print(f"Port {PORT} is busy, trying {PORT+1}")
with socketserver.TCPServer(("", PORT+1), Handler) as httpd:
print(f"Serving at http://localhost:{PORT+1}")
httpd.serve_forever()