From 66b8170bc2f6b80c3021235fa088b8a0509ad966 Mon Sep 17 00:00:00 2001 From: Jarrett Minton Date: Wed, 27 Aug 2025 15:31:39 -0600 Subject: [PATCH] Update server.py --- server.py | 130 +++++++++++++++++++++++++----------------------------- 1 file changed, 61 insertions(+), 69 deletions(-) diff --git a/server.py b/server.py index 0f93cb5..c520cff 100644 --- a/server.py +++ b/server.py @@ -1,70 +1,62 @@ -#!/usr/bin/env python3 -import http.server -import socketserver -import socket -import threading -import webbrowser -from datetime import datetime - -def get_local_ip(): - """Get the local IPv4 address""" - try: - # Connect to a remote server to determine local IP - with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: - # Use Google's DNS server - s.connect(("8.8.8.8", 80)) - local_ip = s.getsockname()[0] - return local_ip - except Exception: - return "127.0.0.1" - -def start_server(port=8000): - """Start HTTP server on specified port""" - local_ip = get_local_ip() - - print(f"Starting HTTP server...") - print(f"Local IPv4 address: {local_ip}") - print(f"Server running on:") - print(f" Local: http://localhost:{port}") - print(f" Network: http://{local_ip}:{port}") - print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") - print(f"\nPress Ctrl+C to stop the server") - print("-" * 50) - - # Custom handler to show request info - class CustomHandler(http.server.SimpleHTTPRequestHandler): - def log_message(self, format, *args): - print(f"[{datetime.now().strftime('%H:%M:%S')}] {format % args}") - - try: - with socketserver.TCPServer(("", port), CustomHandler) as httpd: - # Try to open browser automatically - threading.Timer(1.0, lambda: webbrowser.open(f"http://localhost:{port}")).start() - - httpd.serve_forever() - except KeyboardInterrupt: - print(f"\n\nServer stopped at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") - except OSError as e: - if "Address already in use" in str(e): - print(f"Error: Port {port} is already in use. Try a different port.") - else: - print(f"Error starting server: {e}") - -if __name__ == "__main__": - import sys - - # Default port - port = 8000 - - # Check if port is provided as command line argument - if len(sys.argv) > 1: - try: - port = int(sys.argv[1]) - if port < 1 or port > 65535: - raise ValueError("Port must be between 1 and 65535") - except ValueError as e: - print(f"Invalid port: {e}") - print("Usage: python script.py [port]") - sys.exit(1) - +#!/usr/bin/env python3 +import http.server +import socketserver +import socket +import threading +import webbrowser +from datetime import datetime + +def get_local_ip(): + """Get the local IPv4 address""" + try: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.connect(("8.8.8.8", 80)) + local_ip = s.getsockname()[0] + return local_ip + except Exception: + return "127.0.0.1" + +def start_server(port=8000): + """Start HTTP server on specified port""" + local_ip = get_local_ip() + + print(f"Starting HTTP server...") + print(f"Local IPv4 address: {local_ip}") + print(f"Server running on:") + print(f" Local: http://localhost:{port}") + print(f" Network: http://{local_ip}:{port}") + print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + print(f"\nPress Ctrl+C to stop the server") + print("-" * 50) + + class CustomHandler(http.server.SimpleHTTPRequestHandler): + def log_message(self, format, *args): + print(f"[{datetime.now().strftime('%H:%M:%S')}] {format % args}") + + try: + with socketserver.TCPServer(("", port), CustomHandler) as httpd: + threading.Timer(1.0, lambda: webbrowser.open(f"http://localhost:{port}")).start() + + httpd.serve_forever() + except KeyboardInterrupt: + print(f"\n\nServer stopped at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + except OSError as e: + if "Address already in use" in str(e): + print(f"Error: Port {port} is already in use. Try a different port.") + else: + print(f"Error starting server: {e}") + +if __name__ == "__main__": + import sys + port = 8000 + if len(sys.argv) > 1: + try: + port = int(sys.argv[1]) + if port < 1 or port > 65535: + raise ValueError("Port must be between 1 and 65535") + except ValueError as e: + print(f"Invalid port: {e}") + print("Usage: python script.py [port]") + sys.exit(1) + start_server(port) \ No newline at end of file