feat: 数采做成单独服务3

This commit is contained in:
caoqianming 2025-07-14 16:39:11 +08:00
parent 9e62bf8d41
commit 8ba233f2fd
1 changed files with 15 additions and 8 deletions

View File

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