[Twisted-Python] Noob Question

Peter Sabaini peter at sabaini.at
Sat Mar 14 14:37:15 EDT 2009

On Saturday 14 March 2009 18:33:02 Shelby Ramsey wrote:
> Hello:
> I've been playing with twisted (to take advantage of deferreds) and trying
> to accomplish this:
>   -- log into a machine as a client
>   -- send auth
>   -- receive "events" from the machine
> The events have a Content-Length: \d+ as the protocol for determining the
> length of the message.  What I'm struggling with is how to parse that and
> then receive just that length as an individual message ...

Not really sure if I interpret this correctly. Is your protocol text or 
binary? "Content-Length: \d+" seems to imply ASCII; if thats the case I guess 
you can just do something along the line of:

>>> import re
>>> p = re.compile(r'^Content-Length: (\d+)')
>>> m = p.search("Content-Length: 123")
>>> int(m.group(1))


If its a binary protocol, look at the struct module and the functions pack() 
and unpack()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20090314/bbd475cd/attachment.pgp 

More information about the Twisted-Python mailing list