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

Tycho Andersen tycho at tycho.ws
Sat Jul 18 17:52:41 EDT 2009


On Sat, Jul 18, 2009 at 4:43 PM, Irmen de Jong<irmen.NOSPAM at xs4all.nl> wrote:
> 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.

Exactly, sending the length first is the only way to know ahead of
time. Alternatively, if you know what the end of the data looks like,
you can look for that 'flag' as well, and stop trying to recv() after
that.

Some things that may be useful, though, are socket.settimeout() and
socket.setblocking(). More information is availible in the docs:
http://docs.python.org/library/socket.html.

You need to be careful with this, though, since network latency may
cause problems. Using these methods will keep your program from
sitting in recv() forever, though.

\t
--
http://tycho.ws



More information about the Python-list mailing list