From 216984792689d0fff85c1216f0124bd919b0b43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Czj=E2=80=9D?= <1052308357@qq.com> Date: Sat, 4 Apr 2026 21:15:36 +0800 Subject: [PATCH] add more log --- templates/listener_tcp.py.tpl | 40 +++++++++++++++++++++++++++++------ templates/listener_udp.py.tpl | 38 +++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/templates/listener_tcp.py.tpl b/templates/listener_tcp.py.tpl index 96531c0..de4a418 100644 --- a/templates/listener_tcp.py.tpl +++ b/templates/listener_tcp.py.tpl @@ -33,11 +33,15 @@ COMMAND_MAPPINGS = {command_mappings} def process_command(buf): """处理接收到的命令,返回对应的操作指令""" buf = buf.strip() + logging.debug("process_command: 原始输入='%s', 处理后='%s'" % (repr(buf), buf)) # 状态查询或心跳检测,返回特殊标记 if buf == "state=?" or buf == "": + logging.debug("process_command: 匹配到状态查询/心跳检测") return "status_check" if buf in COMMAND_MAPPINGS: + logging.debug("process_command: 匹配到指令映射 '%s' -> '%s'" % (buf, COMMAND_MAPPINGS[buf])) return COMMAND_MAPPINGS[buf] + logging.debug("process_command: 未知指令 '%s'" % buf) return "nodata" @@ -57,10 +61,15 @@ def main(): print(f"[{{DEVICE_ID}}] 服务启动,监听 {{TMS_SERVER_IP}}:{{TMS_PORT}}") while True: + logging.debug("等待客户端连接...") connection, address = sock.accept() + logging.info("客户端连接建立: %s:%s" % (address[0], address[1])) + try: connection.settimeout(5) + logging.debug("设置超时 5 秒,等待接收数据...") buf_bytes = connection.recv(BUFSIZE) + logging.debug("收到原始数据: %s (长度=%d)" % (repr(buf_bytes), len(buf_bytes))) buf = buf_bytes.decode('utf-8').strip() datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) @@ -70,18 +79,24 @@ def main(): print(logstr) operation = process_command(buf) + logging.debug("process_command 返回: '%s'" % operation) if operation == "status_check": # 状态查询或心跳检测,直接回复 ok - logstr = "===" + datetime_str + " 状态查询/心跳检测,回复 ok" + response = 'ok' + logstr = "===" + datetime_str + " 状态查询/心跳检测,回复: " + response logging.info(logstr) print(logstr) - connection.send(b'ok') + logging.debug("发送响应: '%s'" % response) + connection.send(response.encode('utf-8')) + logging.debug("响应发送完成") elif operation == "nodata": + response = 'Unknown command: %s' % buf logstr = "===" + datetime_str + " 未知指令: '" + str(buf) + "'" logging.warning(logstr) print(logstr) - connection.send(('Unknown command: %s' % buf).encode('utf-8')) + logging.debug("发送未知指令响应: '%s'" % response) + connection.send(response.encode('utf-8')) else: logstr = "===" + datetime_str + " 映射到内部指令: '" + str(operation) + "'" logging.info(logstr) @@ -91,8 +106,15 @@ def main(): str(UPC_DEV_IP) + " " + str(UPC_DEV_PORT) + " " + \ str(operation) + " " + str(LOG_FILE) logging.info("执行外部命令: %s" % os_command) - os.system(os_command) - connection.send(('Command %s processed as %s' % (buf, operation)).encode('utf-8')) + logging.debug("开始执行外部命令...") + exit_code = os.system(os_command) + logging.debug("外部命令执行完成,返回码: %d" % exit_code) + + response = 'Command %s processed as %s' % (buf, operation) + logging.debug("发送响应: '%s'" % response) + connection.send(response.encode('utf-8')) + logging.debug("响应发送完成") + except socket.timeout: datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) logstr = "===" + datetime_str + " 客户端连接超时: " + address[0] + ":" + str(address[1]) + "===" @@ -104,8 +126,14 @@ def main(): " 请求时发生错误: " + str(e) + "===" logging.error(logstr, exc_info=True) print("Error processing request:", e) - connection.send(('Error processing command: %s' % str(e)).encode('utf-8')) + try: + error_response = ('Error processing command: %s' % str(e)).encode('utf-8') + logging.debug("发送错误响应: '%s'" % error_response) + connection.send(error_response) + except Exception as send_err: + logging.error("发送错误响应失败: %s" % send_err) finally: + logging.info("关闭客户端连接: %s:%s" % (address[0], address[1])) connection.close() diff --git a/templates/listener_udp.py.tpl b/templates/listener_udp.py.tpl index 6f8b5de..0211662 100644 --- a/templates/listener_udp.py.tpl +++ b/templates/listener_udp.py.tpl @@ -33,8 +33,15 @@ COMMAND_MAPPINGS = {command_mappings} def process_command(buf): """处理接收到的命令,返回对应的操作指令""" buf = buf.strip() + logging.debug("process_command: 原始输入='%s', 处理后='%s'" % (repr(buf), buf)) + # 状态查询或心跳检测,返回特殊标记 + if buf == "state=?" or buf == "": + logging.debug("process_command: 匹配到状态查询/心跳检测") + return "status_check" if buf in COMMAND_MAPPINGS: + logging.debug("process_command: 匹配到指令映射 '%s' -> '%s'" % (buf, COMMAND_MAPPINGS[buf])) return COMMAND_MAPPINGS[buf] + logging.debug("process_command: 未知指令 '%s'" % buf) return "nodata" @@ -55,8 +62,11 @@ def main(): while True: try: + logging.debug("等待UDP数据包...") # UDP 使用 recvfrom 接收数据,同时获取客户端地址 buf_bytes, client_addr = sock.recvfrom(BUFSIZE) + logging.debug("收到原始数据: %s (长度=%d) 来自 %s:%d" % + (repr(buf_bytes), len(buf_bytes), client_addr[0], client_addr[1])) buf = buf_bytes.decode('utf-8').strip() datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) @@ -66,13 +76,24 @@ def main(): print(logstr) operation = process_command(buf) + logging.debug("process_command 返回: '%s'" % operation) - if operation == "nodata": + if operation == "status_check": + # 状态查询或心跳检测,直接回复 ok + response = b'ok' + logstr = "===" + datetime_str + " 状态查询/心跳检测,回复: ok" + logging.info(logstr) + print(logstr) + logging.debug("发送UDP响应到 %s:%d: '%s'" % (client_addr[0], client_addr[1], response)) + sock.sendto(response, client_addr) + logging.debug("UDP响应发送完成") + elif operation == "nodata": + response = ('Unknown command: %s' % buf).encode('utf-8') logstr = "===" + datetime_str + " 未知指令: '" + str(buf) + "'" logging.warning(logstr) print(logstr) - # 发送UDP响应 - sock.sendto(('Unknown command: %s' % buf).encode('utf-8'), client_addr) + logging.debug("发送UDP未知指令响应到 %s:%d: '%s'" % (client_addr[0], client_addr[1], response)) + sock.sendto(response, client_addr) else: logstr = "===" + datetime_str + " 映射到内部指令: '" + str(operation) + "'" logging.info(logstr) @@ -83,10 +104,15 @@ def main(): str(UPC_DEV_IP) + " " + str(UPC_DEV_PORT) + " " + \ str(operation) + " " + str(LOG_FILE) logging.info("执行外部命令: %s" % os_command) - os.system(os_command) + logging.debug("开始执行外部命令...") + exit_code = os.system(os_command) + logging.debug("外部命令执行完成,返回码: %d" % exit_code) + + response = ('Command %s processed as %s' % (buf, operation)).encode('utf-8') + logging.debug("发送UDP响应到 %s:%d: '%s'" % (client_addr[0], client_addr[1], response)) + sock.sendto(response, client_addr) + logging.debug("UDP响应发送完成") - # 发送UDP响应 - sock.sendto(('Command %s processed as %s' % (buf, operation)).encode('utf-8'), client_addr) except Exception as e: datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) logstr = "===" + datetime_str + " 处理UDP请求时发生错误: " + str(e) + "==="