|
@ -1,6 +1,7 @@ |
|
|
import argparse |
|
|
import argparse |
|
|
import asyncio |
|
|
import asyncio |
|
|
import signal |
|
|
import signal |
|
|
|
|
|
import sys |
|
|
import threading |
|
|
import threading |
|
|
from http.server import HTTPServer, SimpleHTTPRequestHandler |
|
|
from http.server import HTTPServer, SimpleHTTPRequestHandler |
|
|
|
|
|
|
|
@ -9,6 +10,7 @@ import socketio |
|
|
from .decorator import register_service |
|
|
from .decorator import register_service |
|
|
from .get_ip import get_ip_address |
|
|
from .get_ip import get_ip_address |
|
|
|
|
|
|
|
|
|
|
|
http_server_thread = None |
|
|
|
|
|
|
|
|
def start_client(host_ip="0.0.0.0", host_port=4999): |
|
|
def start_client(host_ip="0.0.0.0", host_port=4999): |
|
|
@register_service( |
|
|
@register_service( |
|
@ -19,26 +21,37 @@ def start_client(host_ip="0.0.0.0", host_port=4999): |
|
|
host_port=host_port, |
|
|
host_port=host_port, |
|
|
) |
|
|
) |
|
|
def server(port=13373): |
|
|
def server(port=13373): |
|
|
httpd = HTTPServer(("", port), SimpleHTTPRequestHandler) |
|
|
def start_server(): |
|
|
|
|
|
global http_server_thread |
|
|
print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...") |
|
|
print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...") |
|
|
|
|
|
httpd = HTTPServer(("", port), SimpleHTTPRequestHandler) |
|
|
def serve_forever_nonblocking(): |
|
|
|
|
|
httpd.serve_forever() |
|
|
httpd.serve_forever() |
|
|
|
|
|
server_thread = threading.Thread(target=start_server) |
|
|
server_thread = threading.Thread(target=serve_forever_nonblocking) |
|
|
server_thread.daemon = True |
|
|
server_thread.start() |
|
|
server_thread.start() |
|
|
|
|
|
server_thread.join() |
|
|
|
|
|
|
|
|
return server_thread |
|
|
def signal_handler(signal, frame): |
|
|
|
|
|
print("Cleaning up and shutting down...") |
|
|
|
|
|
|
|
|
|
|
|
# If the HTTP server thread is running, shut it down |
|
|
|
|
|
if http_server_thread is not None and http_server_thread.is_alive(): |
|
|
|
|
|
http_server_thread.shutdown() |
|
|
|
|
|
|
|
|
|
|
|
sys.exit(0) |
|
|
|
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, signal_handler) |
|
|
|
|
|
signal.signal(signal.SIGTERM, signal_handler) |
|
|
try: |
|
|
try: |
|
|
server_thread = server() |
|
|
server() |
|
|
while True: |
|
|
signal.pause() |
|
|
|
|
|
except asyncio.exceptions.CancelledError: |
|
|
|
|
|
print("CancelledError") |
|
|
|
|
|
# signal_handler(signal.SIGINT, None) |
|
|
pass |
|
|
pass |
|
|
except (KeyboardInterrupt, asyncio.exceptions.CancelledError): |
|
|
|
|
|
print("Cleaning up and shutting down...") |
|
|
|
|
|
# server_thread.join() |
|
|
|
|
|
finally: |
|
|
finally: |
|
|
print("Shutting down test client...") |
|
|
print("Shutting down test client...") |
|
|
|
|
|
sys.exit(0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
if __name__ == "__main__": |
|
|