-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
45 lines (36 loc) · 1.13 KB
/
server.py
File metadata and controls
45 lines (36 loc) · 1.13 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
#!/usr/bin/env python3
import sys
sys.dont_write_bytecode = True
import http.server
import socketserver
import os
import webbrowser
from pathlib import Path
PORT = 8000
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
super().end_headers()
def main():
# Change to script directory
script_dir = Path(__file__).parent
os.chdir(script_dir)
# Change to src directory for serving files
src_dir = script_dir / 'src'
if src_dir.exists():
os.chdir(src_dir)
Handler = MyHTTPRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"🚀 Server running at http://localhost:{PORT}/")
print("Press Ctrl+C to stop the server")
# Open browser automatically
try:
webbrowser.open(f'http://localhost:{PORT}')
except:
pass
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n👋 Server stopped")
if __name__ == "__main__":
main()