[Twisted-Python] twisted.web response.py questions

Glyph Lefkowitz glyph at twistedmatrix.com
Sun Dec 6 23:52:23 MST 2015


> On Dec 4, 2015, at 4:42 AM, peter <commercials24 at yahoo.de> wrote:
> 
> hi,
> 
> 
> looking over:
> https://twistedmatrix.com/documents/14.0.2/_downloads/response.py
> 
> 
> im not really sure why one would use the line:
> 
> self.remaining  =  1024  *  10
> 
> 
> this suggest to me that one knew what kind of page size was expected...but what if you dont?

Then you'd have to read this expected size out of the content-length header or similar.  This file is just an example.

> wouldnt it make more sense to use someting like this:
> 
> def dataReceived(self, bytes):
>        self.page_content = self.page_content + bytes
> 
> 
> this would sum up all the data until connectionLost is called.
> and in connectionLost():
> 
> def  connectionLost(self,  reason):
>        print  'Finished receiving body:',  reason.getErrorMessage()
>        self.finished.callback(self.page_content)
> 
> 
> and then print it?

This might be a better example to start with, yes.  Please feel free to submit a doc patch doing just that.

> also i dont get why one would use
> 
> return  finished
> 
> 
> in cbRequest. where is this finished returned to?

It's a Deferred, which means that the next callback in the chain waits for it to fire.  This is there so that cbShutdown doesn't fire (and shutdown the reactor) until the response is fully received.

> its called via:
> 
> d.addCallback(cbRequest)
> 
> isnt the result from cbRequest thrown away?
> i would expect the line to read:
> new_deferred = d.addCallback(cbRequest)

It's not a new Deferred, it's the same Deferred; addCallback mutates the Deferred and then returns it.

> thx for your answers

Thanks for using Twisted :).

Hope this helps!

-glyph





More information about the Twisted-Python mailing list