From acbe2d07bf2024673e4bfb2ec2b8e1601fcd5b28 Mon Sep 17 00:00:00 2001 From: cheney Date: Mon, 25 May 2026 11:35:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=BC=E4=B9=8E=E9=A6=96=E9=A1=B5=E8=83=BD?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=EF=BC=8C=E5=85=B6=E4=BB=96=E7=9A=84=E4=B8=8D?= =?UTF-8?q?=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/proxy_client.py | 22 +++++++++++++++++----- proxy/proxy_server.py | 20 ++++++++++++++++---- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/proxy/proxy_client.py b/proxy/proxy_client.py index 58aab87..c86f8fa 100644 --- a/proxy/proxy_client.py +++ b/proxy/proxy_client.py @@ -3,6 +3,7 @@ import socket import threading import sys import time +import inspect TARGET_HOST = 'zentao.sunyard.com.cn' TARGET_PORT = 9788 @@ -10,12 +11,16 @@ TARGET_PORT = 9788 def log_info(source, message): """Log info message""" timestamp = time.strftime("%Y-%m-%d %H:%M:%S") - print("[%s] [INFO] [%s] %s" % (timestamp, source, message)) + frame = inspect.currentframe().f_back + line = frame.f_lineno + print("[%s] [INFO] [%s] (line %d) %s" % (timestamp, source, line, message)) def log_error(source, message): """Log error message""" timestamp = time.strftime("%Y-%m-%d %H:%M:%S") - print("[%s] [ERROR] [%s] %s" % (timestamp, source, message)) + frame = inspect.currentframe().f_back + line = frame.f_lineno + print("[%s] [ERROR] [%s] (line %d) %s" % (timestamp, source, line, message)) def handle_request(b_socket, request_data): """Handle a single request from device B""" @@ -134,8 +139,11 @@ def connect_to_b_device(b_host, b_port): listen_for_requests(sock) - except ConnectionRefusedError: - log_info("System", "Device B not ready (%s:%d), retrying in 3 seconds..." % (b_host, b_port)) + except socket.error as e: + if e.errno == 111 or e.errno == 10061: + log_info("System", "Device B not ready (%s:%d), retrying in 3 seconds..." % (b_host, b_port)) + else: + log_error("System", "Connection error: %s" % str(e)) time.sleep(3) except socket.timeout: log_info("System", "Connection timeout, retrying in 3 seconds...") @@ -165,7 +173,11 @@ def main(): print("Device B address:", b_host, ":", b_port) print("=" * 60) - connect_to_b_device(b_host, b_port) + try: + connect_to_b_device(b_host, b_port) + except KeyboardInterrupt: + log_info("System", "Received Ctrl+C, exiting...") + sys.exit(0) if __name__ == '__main__': main() diff --git a/proxy/proxy_server.py b/proxy/proxy_server.py index dbe7126..91ac699 100644 --- a/proxy/proxy_server.py +++ b/proxy/proxy_server.py @@ -2,6 +2,8 @@ import socket import threading import time +import sys +import inspect PORT_FOR_A = 8080 # Port for device A to connect PORT_FOR_BROWSER = 8081 # Port for browser to connect @@ -13,12 +15,16 @@ a_device_connected = threading.Event() def log_info(source, message): """Log info message""" timestamp = time.strftime("%Y-%m-%d %H:%M:%S") - print("[%s] [INFO] [%s] %s" % (timestamp, source, message)) + frame = inspect.currentframe().f_back + line = frame.f_lineno + print("[%s] [INFO] [%s] (line %d) %s" % (timestamp, source, line, message)) def log_error(source, message): """Log error message""" timestamp = time.strftime("%Y-%m-%d %H:%M:%S") - print("[%s] [ERROR] [%s] %s" % (timestamp, source, message)) + frame = inspect.currentframe().f_back + line = frame.f_lineno + print("[%s] [ERROR] [%s] (line %d) %s" % (timestamp, source, line, message)) def handle_browser(browser_conn, browser_addr): """Handle browser connection""" @@ -208,11 +214,17 @@ def main(): t1 = threading.Thread(target=run_a_device_server) t2 = threading.Thread(target=run_browser_server) + t1.daemon = True + t2.daemon = True t1.start() t2.start() - t1.join() - t2.join() + try: + while True: + time.sleep(1) + except KeyboardInterrupt: + log_info("System", "Received Ctrl+C, exiting...") + sys.exit(0) if __name__ == '__main__': main()