Replies: 2 comments 12 replies
-
|
Have you try to read and send the big file in chunks, say 512 bytes in size? with open(file_path, 'rb') as f:
while True:
chunk = f.read(512)
if not chunk: break
cnn.sendall(chunk) |
Beta Was this translation helpful? Give feedback.
4 replies
-
|
I assume that your client and server are connecting via HTTP. If you are sending data (text) from a file in chunks over HTTP, you need to provide proper HTTP headers in your reply. # a part of _serve_file function
# --- Send headers ---
cnn.write(b"HTTP/1.1 200 OK\r\n")
cnn.write(b"Content-Type: text/html\r\n")
cnn.write(b"Transfer-Encoding: chunked\r\n")
cnn.write(b"Connection: close\r\n")
cnn.write(b"\r\n")
# --- Send file in chunks ---
while True:
chunk = f.read(CHUNK_SIZE)
if not chunk:
break
cnn.write(b"%x\r\n" % len(chunk))
cnn.write(chunk + b"\r\n")
time.sleep(0.01)
# --- End of chunks ---
cnn.write(b"0\r\n\r\n")
# close to release resources
f.close()
cnn.close() |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello there. I'm having similar issues as described by QuirkyCort and OscamSatUser somewhere else regarding speed while sending rather "large" chunks of data. In my case, the code just hangs and the device requires a reset, even with a timeout. I need to transfer a ~30kB file.
So far
socket.sendall(file.read())yields the best results but still hangs when transferring the largest file at random. I've tried using both blocking and non-blocking writes (socket.write) with a 100ms timeout, as well as reading the file in chunks of 256, 512, 1024, 2k and 4k bytes. I've triedwrite,sendall, and even handling the socket withmakefile. All approaches hang.Using
MicroPython v1.26.1 on 2025-09-11; Generic ESP32 module with ESP32. For context, all this runs in a separate thread started withstart_new_thread(_webserver_task, []). I understand_threadis experimental and not really supported but these are the lemons I chose for my lemonade. There is another separate thread also accepting connections on a different port that reads no file that works flawlessly but never transfers or receives data over 512 bytes.More or less code goes like this.
Where:
Beta Was this translation helpful? Give feedback.
All reactions