Discussion:
[Twisted-Python] twisted.web response.py questions
peter
2015-11-29 13:43:52 UTC
Permalink
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?
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?




also i dont get why one would use

return finished


in cbRequest. where is this finished returned to?
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)



thx for your answers
peter
2015-12-04 12:42:55 UTC
Permalink
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?
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?




also i dont get why one would use

return finished


in cbRequest. where is this finished returned to?
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)



thx for your answers
Glyph Lefkowitz
2015-12-07 06:52:23 UTC
Permalink
Post by peter
hi,
https://twistedmatrix.com/documents/14.0.2/_downloads/response.py
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.
Post by peter
self.page_content = self.page_content + bytes
this would sum up all the data until connectionLost is called.
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.
Post by peter
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.
Post by peter
d.addCallback(cbRequest)
isnt the result from cbRequest thrown away?
new_deferred = d.addCallback(cbRequest)
It's not a new Deferred, it's the same Deferred; addCallback mutates the Deferred and then returns it.
Post by peter
thx for your answers
Thanks for using Twisted :).

Hope this helps!

-glyph

Loading...