Update server.py
This commit is contained in:
parent
5f9b85998d
commit
66b8170bc2
130
server.py
130
server.py
@ -1,70 +1,62 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import http.server
|
import http.server
|
||||||
import socketserver
|
import socketserver
|
||||||
import socket
|
import socket
|
||||||
import threading
|
import threading
|
||||||
import webbrowser
|
import webbrowser
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
def get_local_ip():
|
def get_local_ip():
|
||||||
"""Get the local IPv4 address"""
|
"""Get the local IPv4 address"""
|
||||||
try:
|
try:
|
||||||
# Connect to a remote server to determine local IP
|
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
s.connect(("8.8.8.8", 80))
|
||||||
# Use Google's DNS server
|
local_ip = s.getsockname()[0]
|
||||||
s.connect(("8.8.8.8", 80))
|
return local_ip
|
||||||
local_ip = s.getsockname()[0]
|
except Exception:
|
||||||
return local_ip
|
return "127.0.0.1"
|
||||||
except Exception:
|
|
||||||
return "127.0.0.1"
|
def start_server(port=8000):
|
||||||
|
"""Start HTTP server on specified port"""
|
||||||
def start_server(port=8000):
|
local_ip = get_local_ip()
|
||||||
"""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"Starting HTTP server...")
|
print(f"Server running on:")
|
||||||
print(f"Local IPv4 address: {local_ip}")
|
print(f" Local: http://localhost:{port}")
|
||||||
print(f"Server running on:")
|
print(f" Network: http://{local_ip}:{port}")
|
||||||
print(f" Local: http://localhost:{port}")
|
print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||||
print(f" Network: http://{local_ip}:{port}")
|
print(f"\nPress Ctrl+C to stop the server")
|
||||||
print(f"Started at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
print("-" * 50)
|
||||||
print(f"\nPress Ctrl+C to stop the server")
|
|
||||||
print("-" * 50)
|
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
|
def log_message(self, format, *args):
|
||||||
# Custom handler to show request info
|
print(f"[{datetime.now().strftime('%H:%M:%S')}] {format % args}")
|
||||||
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
|
||||||
def log_message(self, format, *args):
|
try:
|
||||||
print(f"[{datetime.now().strftime('%H:%M:%S')}] {format % args}")
|
with socketserver.TCPServer(("", port), CustomHandler) as httpd:
|
||||||
|
threading.Timer(1.0, lambda: webbrowser.open(f"http://localhost:{port}")).start()
|
||||||
try:
|
|
||||||
with socketserver.TCPServer(("", port), CustomHandler) as httpd:
|
httpd.serve_forever()
|
||||||
# Try to open browser automatically
|
except KeyboardInterrupt:
|
||||||
threading.Timer(1.0, lambda: webbrowser.open(f"http://localhost:{port}")).start()
|
print(f"\n\nServer stopped at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||||
|
except OSError as e:
|
||||||
httpd.serve_forever()
|
if "Address already in use" in str(e):
|
||||||
except KeyboardInterrupt:
|
print(f"Error: Port {port} is already in use. Try a different port.")
|
||||||
print(f"\n\nServer stopped at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
else:
|
||||||
except OSError as e:
|
print(f"Error starting server: {e}")
|
||||||
if "Address already in use" in str(e):
|
|
||||||
print(f"Error: Port {port} is already in use. Try a different port.")
|
if __name__ == "__main__":
|
||||||
else:
|
import sys
|
||||||
print(f"Error starting server: {e}")
|
port = 8000
|
||||||
|
if len(sys.argv) > 1:
|
||||||
if __name__ == "__main__":
|
try:
|
||||||
import sys
|
port = int(sys.argv[1])
|
||||||
|
if port < 1 or port > 65535:
|
||||||
# Default port
|
raise ValueError("Port must be between 1 and 65535")
|
||||||
port = 8000
|
except ValueError as e:
|
||||||
|
print(f"Invalid port: {e}")
|
||||||
# Check if port is provided as command line argument
|
print("Usage: python script.py [port]")
|
||||||
if len(sys.argv) > 1:
|
sys.exit(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)
|
start_server(port)
|
Loading…
Reference in New Issue
Block a user