fix: исправить баги web сервера
- IndexError в log_message — убран неправильный формат - Ctrl+C не работал — добавлен signal_handler - Порт не освобождался — добавлен server_close() - allow_reuse_address=True — для быстрого рестарта - Добавлен remote URL в баннер
This commit is contained in:
@@ -13,24 +13,41 @@ import argparse
|
|||||||
import http.server
|
import http.server
|
||||||
import socketserver
|
import socketserver
|
||||||
import os
|
import os
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
PORT = 8080
|
PORT = 8080
|
||||||
SCRIPT_DIR = Path(__file__).parent.resolve()
|
SCRIPT_DIR = Path(__file__).parent.resolve()
|
||||||
|
|
||||||
|
# Global server reference for cleanup
|
||||||
|
httpd = None
|
||||||
|
|
||||||
|
|
||||||
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
||||||
"""Custom handler with better logging."""
|
"""Custom handler with better logging."""
|
||||||
|
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
super().do_GET()
|
super().do_GET()
|
||||||
self.log_message(f"✅ Served: {self.path}")
|
print(f"[WebServer] ✅ {self.requestline} → {self.command} {self.path}")
|
||||||
|
|
||||||
def log_message(self, format, *args):
|
def log_message(self, format, *args):
|
||||||
print(f"[WebServer] {args[0]}")
|
# Suppress default logging
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def signal_handler(sig, frame):
|
||||||
|
"""Handle Ctrl+C gracefully."""
|
||||||
|
print("\n👋 Shutting down...")
|
||||||
|
if httpd:
|
||||||
|
httpd.shutdown()
|
||||||
|
httpd.server_close()
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
global httpd
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='WebSocket Client Web Server')
|
parser = argparse.ArgumentParser(description='WebSocket Client Web Server')
|
||||||
parser.add_argument('--port', type=int, default=PORT, help=f'Port (default: {PORT})')
|
parser.add_argument('--port', type=int, default=PORT, help=f'Port (default: {PORT})')
|
||||||
parser.add_argument('--host', default='0.0.0.0', help='Host (default: 0.0.0.0)')
|
parser.add_argument('--host', default='0.0.0.0', help='Host (default: 0.0.0.0)')
|
||||||
@@ -39,24 +56,34 @@ def main():
|
|||||||
# Change to websocket-service directory
|
# Change to websocket-service directory
|
||||||
os.chdir(SCRIPT_DIR)
|
os.chdir(SCRIPT_DIR)
|
||||||
|
|
||||||
|
# Register signal handlers
|
||||||
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
signal.signal(signal.SIGTERM, signal_handler)
|
||||||
|
|
||||||
|
# Allow socket reuse
|
||||||
|
socketserver.TCPServer.allow_reuse_address = True
|
||||||
|
|
||||||
print(f"""
|
print(f"""
|
||||||
╔══════════════════════════════════════════════════════════╗
|
╔══════════════════════════════════════════════════════════╗
|
||||||
║ 🛰️ Wyoming Satellite WebSocket Client Server ║
|
║ 🛰️ Wyoming Satellite WebSocket Client Server ║
|
||||||
╠══════════════════════════════════════════════════════════╣
|
╠══════════════════════════════════════════════════════════╣
|
||||||
║ Serving: {str(SCRIPT_DIR):<45} ║
|
║ Serving: {str(SCRIPT_DIR):<45} ║
|
||||||
║ URL: http://localhost:{args.port:<36} ║
|
║ URL: http://localhost:{args.port:<36} ║
|
||||||
|
║ Remote: http://<your-ip>:{args.port:<32} ║
|
||||||
║ ║
|
║ ║
|
||||||
║ Press Ctrl+C to stop ║
|
║ Press Ctrl+C to stop ║
|
||||||
╚══════════════════════════════════════════════════════════╝
|
╚══════════════════════════════════════════════════════════╝
|
||||||
""")
|
""")
|
||||||
|
|
||||||
with socketserver.TCPServer((args.host, args.port), CustomHandler) as httpd:
|
with socketserver.TCPServer((args.host, args.port), CustomHandler) as httpd:
|
||||||
try:
|
|
||||||
print(f"🚀 Server started on http://{args.host}:{args.port}")
|
print(f"🚀 Server started on http://{args.host}:{args.port}")
|
||||||
|
try:
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("\n👋 Shutting down...")
|
pass
|
||||||
httpd.shutdown()
|
finally:
|
||||||
|
print("\n👋 Server stopped")
|
||||||
|
httpd.server_close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user