How to receive a data file of unknown length using a python socket?
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? The following is a snippet:
[code]
f = open("frame.jpg",mode = 'wb')
while True:
data = self.s.recv(MAXPACKETLEN)
if len(data) == 0:
break
recvd += len(data)
f.write(data)
f.close()
[end]
It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on
the final packet, which will always be less than MAXPACKETLEN.
I guess my question is, how do I detect end of data on the client side?
--
http://mail.python.org/mailman/listinfo/python-list
Re: How to receive a data file of unknown length using a python socket?
On Jul 18, 4:43 pm, Irmen de Jong 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. > > --irmen Thanks for the reply. But, now I have a newbie Python question. If I send a 4 byte address from the embedded device, how do I convert that, in Python, to a 4 byte, or long, number? -- http://mail.python.org/mailman/listinfo/python-list
Re: How to receive a data file of unknown length using a python socket?
On Jul 18, 7:33 pm, MRAB wrote: > Nobody wrote: > > On Sat, 18 Jul 2009 14:33:48 -0700, twgray wrote: > > >> It appears to be locking up in 'data=self.s.recv(MAXPACKETLEN)' on > >> the final packet, which will always be less than MAXPACKETLEN. > > >> I guess my question is, how do I detect end of data on the client side? > > > recv() should return zero when the sender closes its end of the connection. > > > Is the sender actually closing its end? If you are unsure, use a packet > > sniffer such as tcpdump to look for a packet with the FIN flag. > > > If you need to keep the connection open for further transfers, you need to > > incorporate some mechanism for identifying the end of the data into the > > protocol. As others have suggested, prefixing the data by its length is > > one option. Another is to use an end-of-data marker, but then you need a > > mechanism to "escape" the marker if it occurs in the data. A length prefix > > is probably simpler to implement, but has the disadvantage that you can't > > start sending the data until you know how long it is going to be. > > You could send it in chunks, ending with a chunk length of zero. Thanks for the help! -- http://mail.python.org/mailman/listinfo/python-list
