1 from twisted.python import log
2 from twisted.internet import reactor
3 from twisted.internet.defer import Deferred
4 from twisted.internet.protocol import Protocol
5 from twisted.web.client import Agent, ContentDecoderAgent, GzipDecoder
7 class BeginningPrinter(Protocol):
8 def __init__(self, finished):
9 self.finished = finished
10 self.remaining = 1024 * 10
13 def dataReceived(self, bytes):
15 display = bytes[:self.remaining]
16 print 'Some data received:'
18 self.remaining -= len(display)
21 def connectionLost(self, reason):
22 print 'Finished receiving body:', reason.type, reason.value
23 self.finished.callback(None)
27 def printBody(response):
29 response.deliverBody(BeginningPrinter(finished))
34 agent = ContentDecoderAgent(Agent(reactor), [('gzip', GzipDecoder)])
36 d = agent.request('GET', 'http://www.yahoo.com/')
37 d.addCallback(printBody)
39 d.addCallback(lambda ignored: reactor.stop())
42 if __name__ == "__main__":