-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
50 lines (35 loc) · 1.04 KB
/
Copy pathrun.py
File metadata and controls
50 lines (35 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
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
from __future__ import annotations
import http.server
import socketserver
import threading
import time
import webbrowser
from pathlib import Path
HOST = "127.0.0.1"
PORT = 8000
class ReusableTCPServer(socketserver.TCPServer):
allow_reuse_address = True
def main() -> None:
root = Path(__file__).resolve().parent
if not (root / "index.html").exists():
raise SystemExit("index.html not found.")
handler = lambda *args, **kwargs: http.server.SimpleHTTPRequestHandler(
*args,
directory=str(root),
**kwargs,
)
url = f"http://{HOST}:{PORT}/index.html"
with ReusableTCPServer((HOST, PORT), handler) as server:
print(f"Serving: {url}")
print("Press Ctrl+C to stop.")
threading.Thread(
target=lambda: (time.sleep(0.5), webbrowser.open(url)),
daemon=True,
).start()
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
if __name__ == "__main__":
main()