-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve_dashboard.py
More file actions
61 lines (48 loc) · 1.75 KB
/
serve_dashboard.py
File metadata and controls
61 lines (48 loc) · 1.75 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
51
52
53
54
55
56
57
58
59
60
61
import http.server
import socketserver
import os
import webbrowser
from urllib.parse import urlparse
# Configuration
PORT = 8000
DIRECTORY = "dashboard"
class DashboardHandler(http.server.SimpleHTTPRequestHandler):
"""Custom request handler for the dashboard server."""
def __init__(self, *args, **kwargs):
# Set the directory to serve files from
super().__init__(*args, directory=DIRECTORY, **kwargs)
def do_GET(self):
"""Handle GET requests."""
# Redirect root to index.html
if self.path == '/':
self.path = '/index.html'
# Handle the request
return http.server.SimpleHTTPRequestHandler.do_GET(self)
def log_message(self, format, *args):
"""Log messages with a timestamp."""
print(f"[{self.log_date_time_string()}] {args[0]} {args[1]} {args[2]}")
def main():
"""Main function to start the server."""
# Change to the script's directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Ensure the dashboard directory exists
if not os.path.exists(DIRECTORY):
print(f"Error: Directory '{DIRECTORY}' not found.")
return
# Create the server
handler = DashboardHandler
httpd = socketserver.TCPServer(("", PORT), handler)
# Print server information
print(f"Starting dashboard server at http://localhost:{PORT}")
print(f"Serving files from '{DIRECTORY}' directory")
print("Press Ctrl+C to stop the server")
# Open the browser
webbrowser.open(f"http://localhost:{PORT}")
# Start the server
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server...")
httpd.shutdown()
if __name__ == "__main__":
main()