feat: 数采做成单独服务3
This commit is contained in:
parent
9e62bf8d41
commit
8ba233f2fd
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue