使用H7 Plus发送HTTP报文会提示运行内存不足
-
H7 Plus的额RAM不是32M吗?感觉很容易内存就没了。而且我经过手动内存释放后,还是会有内存不足的提示。
import sensor import network import socket import gc from pyb import UART from pyb import LED led = LED(1) led_p = LED(2) led.on() led_p.off() uart = UART(3, 9600) type_ = "3" type_data =("\r\n" + type_).encode('utf-8') # AP info SSID = "" # Network SSID KEY = "" # Network key PORT = 9999 HOST = "" # 构建 HTTP 请求头和主体 boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW' content_type = 'image/jpg' # 构建请求体 body_start_type = ( "\r\n" "--" + boundary + "\r\n" "Content-Disposition: form-data; name=\"type\"\r\n" ).encode('utf-8') # 构建请求体尾 body_end = ("\r\n--" + boundary + "--\r\n").encode('utf-8') # 初始化感光元件 sensor.reset() sensor.set_pixformat(sensor.RGB565) # Modify as you like. sensor.set_framesize(sensor.SVGA) # Modify as you like. # 初始化wlan模块并连接到网络 wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(SSID, KEY) while not wlan.isconnected(): print('Trying to connect to "{:s}"...'.format(SSID)) time.sleep_ms(1000) led.off() led_p.on() print("WiFi Connected ", wlan.ifconfig()) # 通过DNS获取addr信息 addr = socket.getaddrinfo(HOST, PORT)[0][4] print(addr) del addr while True: read_data = uart.read(10) if read_data != None: if read_data and read_data != b'\x00' * 10: # 检查数据是否全是0 # 解码字节串为字符串 string_data = read_data.decode('utf-8') del read_data # 添加文件扩展名 filename = string_data + ".jpg" del string_data led_p.off() # 拍照 frame = sensor.snapshot() frame.save(filename) del frame f = open(filename, 'rb') file_data = f.read() f.close() del f # 构建请求体 body_start_data = ( "--" + boundary + "\r\n" "Content-Disposition: form-data; name=\"image\"; filename=\"" + filename + "\"\r\n" "Content-Type: " + content_type + "\r\n\r\n" ).encode('utf-8') del filename # 完整的请求体 body = (body_start_data + file_data + body_start_type + type_data + body_end) del body_start_data del file_data # 构建请求头 headers = ( "POST /images/upload HTTP/1.1\r\n" "Host: " + HOST + ":" + str(PORT) + "\r\n" "Content-Length: " + str(len(body)) + "\r\n" "Content-Type: multipart/form-data; boundary=" + boundary + "\r\n" "Connection: keep-alive\r\n\r\n" ).encode('utf-8') # 完整的请求 request_data = headers + body del headers del body # 创建 socket 连接 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((HOST, PORT)) # 发送 HTTP 请求 sock.sendall(request_data) del request_data # 接收服务器响应 data = sock.recv(4096) sock.close() del sock led_p.on() gc.collect()
-
你先保存文件,然后再读取,这个操作太奇怪。你应该用to_jpeg(copy=True)