[Twisted-Python] Noob Question

Peter Sabaini peter at sabaini.at
Sat Mar 14 13:55:01 MDT 2009


On Saturday 14 March 2009 19:54:04 Shelby Ramsey wrote:
> Peter,
>
> Thanks for the quick response.  The issue isn't grabbing the content length
> ... but rather grabbing the body ...
>
> The protocol is TCP so the text (not binary) comes in streams ... so the
> body is / might be broken into streams ... so I'm trying to figure out how
> to "receive" the content length so I can get a complete body from different
> streams.

Still not sure if I understand -- with TCP, there is always just one stream, 
and so its just a matter of breaking up the stream into suitable chunks.  

And in your case, it seems you have something like HTTP, ie. some header lines 
including Content-Length: and then the body, is that correct?

If that is the case, the twisted.protocols.basic.LineReceiver class might be a 
good starting point. To use it, you make your Protocol handler a subclass of 
LineReceiver; the reactor then calls your protocol objects' lineReceived() 
method with new data as long as you stay in "line mode". Switch into "raw 
mode" to receive the body of the message. With HTTP for example, this would be 
after you receive an empty line. You do this by calling the setRawmode() 
method which is provided by LineReceiver. From that point on forward, the 
reactor will call the dataReceived() method to hand your protocol object new 
data. Presumably, you've already received a Content-Length header and stored 
that somewhere. Receive Content-Length bytes of data, and call setLineMode() 
to switch into line mode again. 

API docs are 
here:http://twistedmatrix.com/documents/current/api/twisted.protocols.basic.LineReceiver.html

Hm, that would maybe be a good topic for a tutorial.

hth,
peter.



> Thanks again!
>
> SDR

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: </pipermail/twisted-python/attachments/20090314/09dc32f9/attachment.sig>


More information about the Twisted-Python mailing list