From 5f9b85998d1d3b954bb0560c411121f8ad7b2ced Mon Sep 17 00:00:00 2001 From: Jarrett Minton Date: Wed, 27 Aug 2025 15:30:45 -0600 Subject: [PATCH] Upload files to "/" --- server.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 server.py diff --git a/server.py b/server.py new file mode 100644 index 0000000..0f93cb5 --- /dev/null +++ b/server.py @@ -0,0 +1,70 @@ +#!/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) + + start_server(port) \ No newline at end of file