[Twisted-Python] How to abuse generators, now that we're allowed to play with 2.2

Bob Ippolito bob at redivi.com
Fri May 23 19:57:22 EDT 2003


Here's some code I've been playing with the past two days.. part of a 
DAAP (Digital Audio Access Protocol, aka iTunes 4.0) client/server 
framework that I'm working on that's nowhere near releasable at the 
moment..

However, the gzip/gunzip streaming code and the example of generator 
abuse may be of immediate use to someone in the meantime, so here you 
go.

HTTPStream expects you to pass in a protocol that will receive the page 
incrementally.  Try passing in headers={'Accept-Encoding':'gzip'} and 
talking to your favorite server, it may actually decide to send you the 
page gzipped.. with the way the protocol stack works, your underlying 
protocol won't even know the difference.  It can of course be extended 
to support any encoding(s) with the current dispatch mechanism, but 
gzip is the only one I need at the moment.

As far as IterableBytestreamProtocol goes, you should be careful if you 
use it.. basically it will work perfectly until connectionLost happens, 
and then it'll start throwing back None to your read requests (which 
will never happen otherwise).  If you set iterateOnLoss = False, then 
your iterator will just stop running when you lose connection, which 
may or may not be desirable.  Use at your own risk, but feel free to 
improve on it if you have any suggestions.

-bob


-------------- next part --------------
A non-text attachment was scrubbed...
Name: gzstream.py
Type: application/octet-stream
Size: 8149 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030523/a7d9d5a6/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: util.py
Type: application/octet-stream
Size: 2150 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030523/a7d9d5a6/attachment-0001.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: httpstream.py
Type: application/octet-stream
Size: 1554 bytes
Desc: not available
Url : http://twistedmatrix.com/pipermail/twisted-python/attachments/20030523/a7d9d5a6/attachment-0002.obj 


More information about the Twisted-Python mailing list