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()