添加现有代码V1.6
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
import urequests
|
||||
import gc
|
||||
import socket
|
||||
import _thread
|
||||
import time
|
||||
import json
|
||||
import network
|
||||
|
||||
local_ip = network.LAN().ifconfig()[0]
|
||||
controller_ip = "192.168.100.10"
|
||||
http_server_port = 10008
|
||||
udp_port_up = 11200
|
||||
|
||||
udp_port_local = 11300
|
||||
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
udp_socket.settimeout(0)
|
||||
udp_socket.bind((local_ip, udp_port_local))
|
||||
|
||||
local_cmd = None
|
||||
local_cmd_flag = False
|
||||
|
||||
def httpc_post_data_json_bin(info, data):
|
||||
"""发送混合格式数据:JSON元数据 + 二进制data"""
|
||||
import usocket
|
||||
|
||||
# 1. 构建multipart边界(随机字符串,避免与data内容冲突)
|
||||
boundary = "----WebKitFormBoundary" + "a1b2c3d4e5f6"
|
||||
|
||||
# 2. 构建JSON元数据部分(轻量,RAM占用小)
|
||||
json_part = (
|
||||
f"--{boundary}\r\n"
|
||||
'Content-Disposition: form-data; name="metadata"\r\n'
|
||||
'Content-Type: application/json\r\n'
|
||||
'\r\n' # 空行分隔头和内容
|
||||
f"{json.dumps(info)}\r\n"
|
||||
)
|
||||
|
||||
# 3. 构建二进制数据部分头(仅头部,不包含data本身)
|
||||
bin_part_header = (
|
||||
f"--{boundary}\r\n"
|
||||
'Content-Disposition: form-data; name="data"; filename="binary_data.bin"\r\n'
|
||||
'Content-Type: application/octet-stream\r\n'
|
||||
'\r\n' # 空行分隔头和内容
|
||||
)
|
||||
|
||||
# 4. 构建结束边界
|
||||
end_boundary = f"\r\n--{boundary}--\r\n"
|
||||
|
||||
# 5. 计算总长度
|
||||
content_length = len(json_part) + len(bin_part_header) + len(data) + len(end_boundary)
|
||||
|
||||
# 6. 构建HTTP请求头(逐步发送,避免大字符串占用RAM)
|
||||
sock = usocket.socket()
|
||||
if not sock:
|
||||
print('创建socket失败')
|
||||
return False
|
||||
sock.settimeout(0.10)
|
||||
|
||||
controller_ip = '192.168.100.10'
|
||||
|
||||
try:
|
||||
sock.connect((controller_ip, http_server_port))
|
||||
except:
|
||||
return False
|
||||
|
||||
# 8. 接收响应(可选)
|
||||
try:
|
||||
sock.settimeout(0.5)
|
||||
# 发送请求行
|
||||
sock.write(b"POST /api/endpoint HTTP/1.1\r\n")
|
||||
|
||||
# 发送固定头
|
||||
sock.write(f"Host: {controller_ip}\r\n".encode())
|
||||
sock.write(b"Connection: close\r\n")
|
||||
|
||||
# 发送Content-Type和Content-Length
|
||||
sock.write(f"Content-Type: multipart/form-data; boundary={boundary}\r\n".encode())
|
||||
sock.write(f"Content-Length: {content_length}\r\n\r\n".encode())
|
||||
|
||||
# 7. 分块发送数据(核心优化:避免构建完整请求体)
|
||||
sock.write(json_part.encode()) # 发送JSON元数据
|
||||
sock.write(bin_part_header.encode())# 发送二进制数据头
|
||||
sock.write(data) # 直接发送二进制data(无额外编码)
|
||||
sock.write(end_boundary.encode()) # 发送结束边界
|
||||
|
||||
response = sock.readline()
|
||||
print('response:', response)
|
||||
except:
|
||||
print('接收响应失败')
|
||||
return False
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
if not response:
|
||||
print('接收响应失败')
|
||||
return False
|
||||
|
||||
return b"200 OK" in response
|
||||
|
||||
#data为字节数组或者字符串
|
||||
def httpc_post_data(data):
|
||||
try:
|
||||
default_header = {
|
||||
'Content-Type': 'application/json',
|
||||
}
|
||||
response = urequests.post(f"http://{controller_ip}:{http_server_port}", data=data, headers=default_header)
|
||||
|
||||
return response.status_code == 200
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return False
|
||||
finally:
|
||||
try:
|
||||
response.close()
|
||||
except:
|
||||
pass
|
||||
gc.collect()
|
||||
|
||||
def udp_send(data):
|
||||
try:
|
||||
udp_socket.sendto(data, (controller_ip, udp_port_up))
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
def local_cmd_handle(data):
|
||||
global local_cmd, local_cmd_flag
|
||||
if data:
|
||||
local_cmd = data
|
||||
local_cmd_flag = True
|
||||
|
||||
def udp_recv():
|
||||
global local_cmd, local_cmd_flag
|
||||
if local_cmd_flag:
|
||||
local_cmd_flag = False
|
||||
return local_cmd
|
||||
|
||||
try:
|
||||
data, addr = udp_socket.recvfrom(1024)
|
||||
if data:
|
||||
print('recv:', data)
|
||||
return data
|
||||
except:
|
||||
return None
|
||||
|
||||
__exit_flag = False
|
||||
|
||||
def udp_recv_thread(handler):
|
||||
udp_socket.settimeout(1)
|
||||
while not __exit_flag:
|
||||
time.sleep_ms(1000)
|
||||
data = udp_recv()
|
||||
print('udp recv timeout')
|
||||
|
||||
if data is None:
|
||||
continue
|
||||
if handler is None:
|
||||
continue
|
||||
try:
|
||||
handler(data)
|
||||
except:
|
||||
pass
|
||||
udp_socket.close()
|
||||
|
||||
def stop_udp_thread():
|
||||
global __exit_flag
|
||||
__exit_flag = True
|
||||
|
||||
def start_udp_thread(handler):
|
||||
_thread.stack_size(20 * 1024)
|
||||
return _thread.start_new_thread(udp_recv_thread, (handler,))
|
||||
|
||||
Reference in New Issue
Block a user