update refactoring and add new features
This commit is contained in:
+12
-5
@@ -1,9 +1,11 @@
|
||||
"""Simple HTTP file server for serving files in web mode."""
|
||||
|
||||
import atexit
|
||||
import os
|
||||
import threading
|
||||
import socket
|
||||
import logging
|
||||
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
||||
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
import mimetypes
|
||||
@@ -11,9 +13,12 @@ import urllib.parse
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_DNS_FALLBACK_HOST = os.environ.get("MM_DNS_SERVER", "8.8.8.8")
|
||||
_DNS_FALLBACK_PORT = 80
|
||||
|
||||
# Global server instance
|
||||
_file_server: Optional[HTTPServer] = None
|
||||
_server_thread: Optional[threading.Thread] = None
|
||||
_file_server: Optional[ThreadingHTTPServer] = None
|
||||
_file_server_thread: Optional[threading.Thread] = None
|
||||
_server_port: int = 8001
|
||||
|
||||
|
||||
@@ -84,7 +89,7 @@ def get_local_ip() -> Optional[str]:
|
||||
try:
|
||||
# Connect to a remote server to determine local IP
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
s.connect(("8.8.8.8", 80))
|
||||
s.connect((_DNS_FALLBACK_HOST, _DNS_FALLBACK_PORT))
|
||||
ip = s.getsockname()[0]
|
||||
s.close()
|
||||
return ip
|
||||
@@ -116,7 +121,7 @@ def start_file_server(port: int = 8001) -> Optional[str]:
|
||||
|
||||
# Create server
|
||||
server_address = ("", port)
|
||||
_file_server = HTTPServer(server_address, FileServerHandler)
|
||||
_file_server = ThreadingHTTPServer(server_address, FileServerHandler)
|
||||
|
||||
# Start in daemon thread
|
||||
_server_thread = threading.Thread(
|
||||
@@ -125,6 +130,8 @@ def start_file_server(port: int = 8001) -> Optional[str]:
|
||||
)
|
||||
_server_thread.start()
|
||||
|
||||
atexit.register(stop_file_server)
|
||||
|
||||
logger.info(f"File server started on port {port}")
|
||||
|
||||
# Get local IP
|
||||
|
||||
Reference in New Issue
Block a user