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 socketserver
|
||||
import os
|
||||
import signal
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
PORT = 8080
|
||||
SCRIPT_DIR = Path(__file__).parent.resolve()
|
||||
|
||||
# Global server reference for cleanup
|
||||
httpd = None
|
||||
|
||||
|
||||
class CustomHandler(http.server.SimpleHTTPRequestHandler):
|
||||
"""Custom handler with better logging."""
|
||||
|
||||
def do_GET(self):
|
||||
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):
|
||||
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():
|
||||
global httpd
|
||||
|
||||
parser = argparse.ArgumentParser(description='WebSocket Client Web Server')
|
||||
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)')
|
||||
@@ -39,24 +56,34 @@ def main():
|
||||
# Change to websocket-service directory
|
||||
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"""
|
||||
╔══════════════════════════════════════════════════════════╗
|
||||
║ 🛰️ Wyoming Satellite WebSocket Client Server ║
|
||||
╠══════════════════════════════════════════════════════════╣
|
||||
║ Serving: {str(SCRIPT_DIR):<45} ║
|
||||
║ URL: http://localhost:{args.port:<36} ║
|
||||
║ Remote: http://<your-ip>:{args.port:<32} ║
|
||||
║ ║
|
||||
║ Press Ctrl+C to stop ║
|
||||
╚══════════════════════════════════════════════════════════╝
|
||||
""")
|
||||
|
||||
with socketserver.TCPServer((args.host, args.port), CustomHandler) as httpd:
|
||||
print(f"🚀 Server started on http://{args.host}:{args.port}")
|
||||
try:
|
||||
print(f"🚀 Server started on http://{args.host}:{args.port}")
|
||||
httpd.serve_forever()
|
||||
except KeyboardInterrupt:
|
||||
print("\n👋 Shutting down...")
|
||||
httpd.shutdown()
|
||||
pass
|
||||
finally:
|
||||
print("\n👋 Server stopped")
|
||||
httpd.server_close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user