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