-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_server.py
72 lines (56 loc) · 2.1 KB
/
start_server.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# import http.server
# import socketserver
# import webbrowser
# import threading
# PORT = 8000
# # Start a simple HTTP server
# class CustomHandler(http.server.SimpleHTTPRequestHandler):
# def end_headers(self):
# self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
# self.send_header('Pragma', 'no-cache')
# self.send_header('Expires', '0')
# super().end_headers()
# def start_server():
# with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
# print(f"Serving at http://localhost:{PORT}")
# httpd.serve_forever()
# # Open the browser automatically
# def open_browser():
# webbrowser.open(f"http://localhost:{PORT}/index.html")
# # Run the server in a separate thread so it doesn't block the browser opening
# threading.Thread(target=start_server, daemon=True).start()
# open_browser()
# # Keep script running
# input("Press Enter to exit...\n")
import http.server
import socketserver
import webbrowser
import threading
import os
import sys
PORT = 8000
# Get the directory of the EXE or script
if getattr(sys, 'frozen', False):
BASE_DIR = os.path.dirname(sys.executable) # When running as an EXE
else:
BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # When running as a script
# Change working directory to serve files correctly
os.chdir(BASE_DIR)
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header('Cache-Control', 'no-cache, no-store, must-revalidate')
self.send_header('Pragma', 'no-cache')
self.send_header('Expires', '0')
super().end_headers()
def start_server():
with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
print(f"Serving at http://localhost:{PORT}")
httpd.serve_forever()
# Open the browser automatically
def open_browser():
webbrowser.open(f"http://localhost:{PORT}/index.html")
# Run the server in a separate thread so it doesn't block the browser opening
threading.Thread(target=start_server, daemon=True).start()
open_browser()
# Keep script running
input("Press Enter to exit...\n")