Update server.py
This commit is contained in:
parent
5f9b85998d
commit
66b8170bc2
@ -9,9 +9,7 @@ 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:
|
||||||
# Use Google's DNS server
|
|
||||||
s.connect(("8.8.8.8", 80))
|
s.connect(("8.8.8.8", 80))
|
||||||
local_ip = s.getsockname()[0]
|
local_ip = s.getsockname()[0]
|
||||||
return local_ip
|
return local_ip
|
||||||
@ -31,14 +29,12 @@ def start_server(port=8000):
|
|||||||
print(f"\nPress Ctrl+C to stop the server")
|
print(f"\nPress Ctrl+C to stop the server")
|
||||||
print("-" * 50)
|
print("-" * 50)
|
||||||
|
|
||||||
# Custom handler to show request info
|
|
||||||
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
def log_message(self, format, *args):
|
def log_message(self, format, *args):
|
||||||
print(f"[{datetime.now().strftime('%H:%M:%S')}] {format % args}")
|
print(f"[{datetime.now().strftime('%H:%M:%S')}] {format % args}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with socketserver.TCPServer(("", port), CustomHandler) as httpd:
|
with socketserver.TCPServer(("", port), CustomHandler) as httpd:
|
||||||
# Try to open browser automatically
|
|
||||||
threading.Timer(1.0, lambda: webbrowser.open(f"http://localhost:{port}")).start()
|
threading.Timer(1.0, lambda: webbrowser.open(f"http://localhost:{port}")).start()
|
||||||
|
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
@ -52,11 +48,7 @@ def start_server(port=8000):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
# Default port
|
|
||||||
port = 8000
|
port = 8000
|
||||||
|
|
||||||
# Check if port is provided as command line argument
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
try:
|
try:
|
||||||
port = int(sys.argv[1])
|
port = int(sys.argv[1])
|
||||||
|
Loading…
Reference in New Issue
Block a user