How to receive a data file of unknown length using a python socket?

Irmen de Jong irmen.NOSPAM at xs4all.nl
Sat Jul 18 17:43:47 EDT 2009


twgray wrote:
> I am attempting to send a jpeg image file created on an embedded
> device over a wifi socket to a Python client running on a Linux pc
> (Ubuntu).  All works well, except I don't know, on the pc client side,
> what the file size is?  

You don't. Sockets are just endless streams of bytes. You will have to design some form 
of 'wire protocol' that includes the length of the message that is to be read.
For instance a minimalistic protocol could be the following:
Send 4 bytes that contain the length (an int) then the data itself. The client reads 4 
bytes, decodes it into the integer that tells it the length, and then reads the correct 
amount of bytes from the socket.


--irmen



More information about the Python-list mailing list