-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.py
More file actions
executable file
·49 lines (38 loc) · 1.46 KB
/
serve.py
File metadata and controls
executable file
·49 lines (38 loc) · 1.46 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
#!/usr/bin/env python3
"""Simple HTTP server for Rayforce PyLab development"""
import http.server
import socketserver
import os
import sys
PORT = 8080
class CORSHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
"""HTTP handler with CORS and proper MIME types for WASM"""
extensions_map = {
**http.server.SimpleHTTPRequestHandler.extensions_map,
'.wasm': 'application/wasm',
'.js': 'application/javascript',
'.mjs': 'application/javascript',
}
def end_headers(self):
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, OPTIONS')
self.send_header('Access-Control-Allow-Headers', '*')
self.send_header('Cross-Origin-Opener-Policy', 'same-origin')
self.send_header('Cross-Origin-Embedder-Policy', 'require-corp')
super().end_headers()
def do_OPTIONS(self):
self.send_response(200)
self.end_headers()
def main():
# Serve from the rayforce-pylab directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
with socketserver.TCPServer(("", PORT), CORSHTTPRequestHandler) as httpd:
print(f"Serving at http://localhost:{PORT}")
print(f"Open http://localhost:{PORT}/index.html")
print("Press Ctrl+C to stop")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nStopping server...")
if __name__ == "__main__":
main()