Re: struct.unpack() on a stream
En Fri, 27 Feb 2009 09:29:16 -0200, Ulrich Eckhardt eckha...@satorlaser.com 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. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: struct.unpack() on a stream
Gabriel Genellina wrote: En Fri, 27 Feb 2009 09:29:16 -0200, Ulrich Eckhardt eckha...@satorlaser.com 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) 2 That will tell you how many bytes to read. -- http://mail.python.org/mailman/listinfo/python-list
Re: struct.unpack() on a stream
En Fri, 27 Feb 2009 10:10:39 -0200, MRAB goo...@mrabarnett.plus.com escribió: Gabriel Genellina wrote: En Fri, 27 Feb 2009 09:29:16 -0200, Ulrich Eckhardt eckha...@satorlaser.com 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) 2 That will tell you how many bytes to read. The struct module defines also a Struct class; instead of using the module functions, it's more efficient to create a Struct instance and call its methods when one is going to use the same format more than once: py s = struct.Struct(H) py s.size 2 py s.unpack(\x40\x00) (64,) That's the size attribute I was talking about - too tersely perhaps. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list