struct.unpack() on a stream

MRAB google at
Fri Feb 27 07:10:39 EST 2009

Gabriel Genellina wrote:
> En Fri, 27 Feb 2009 09:29:16 -0200, Ulrich Eckhardt 
> <eckhardt at> escribió:
>> I have a socket from which I would like to parse some data, how would 
>> I do
>> that? Of course, I can manually read data from the socket until unpack()
>> stops complaining about a lack of data, but that sounds rather inelegant.
>> Any better suggestions?
> Read until you get the required bytes; use the size attribute.
The struct module has a function called "calcsize", eg:

 >>> import struct
 >>> struct.calcsize("<H")

That will tell you how many bytes to read.

More information about the Python-list mailing list