diff --git a/out_service/cd.py b/out_service/cd.py index b50f8dc7..2618e130 100644 --- a/out_service/cd.py +++ b/out_service/cd.py @@ -2,6 +2,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer import json from urllib.parse import urlparse, parse_qs import socket +import traceback sc_all = { "192.168.1.220_6000": None, @@ -69,25 +70,31 @@ class JSONRequestHandler(BaseHTTPRequestHandler): if addr not in sc_all: self.error(f'{addr} 未找到') return - sc = None + def connect_and_send(): - nonlocal sc sc = sc_all[addr] try: if sc is None: sc = socket.socket() sc.settimeout(5) # 设置超时 sc.connect((host, int(port))) - sc_all[f"{host}_{port}"] = sc + sc_all[addr] = sc sc.sendall(b"R") + return sc except Exception as e: - try: - sc.close() - except Exception: - pass + if sc is not None: + try: + sc.close() + except Exception: + pass + sc_all[addr] = None self.error(f'采集器连接失败: {e}') + print(traceback.format_exc()) + return None - connect_and_send() + sc = connect_and_send() + if sc is None: + return resp = sc.recv(1024) res = handle_bytes(resp) if isinstance(res, str):