-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve_viewer.py
More file actions
38 lines (30 loc) · 1.04 KB
/
serve_viewer.py
File metadata and controls
38 lines (30 loc) · 1.04 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
#!/usr/bin/env python3
import http.server
import socketserver
import webbrowser
import os
from pathlib import Path
PORT = 8000
class Handler(http.server.SimpleHTTPRequestHandler):
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', '*')
super().end_headers()
def log_message(self, format, *args):
return
def main():
os.chdir(Path(__file__).parent)
socketserver.TCPServer.allow_reuse_address = True
with socketserver.TCPServer(("", PORT), Handler) as httpd:
url = f"http://localhost:{PORT}/index.html"
print(f"Starting server at http://localhost:{PORT}")
print(f"Opening viewer at {url}")
print("Press Ctrl+C to stop the server")
webbrowser.open(url)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
if __name__ == "__main__":
main()