Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / web / howto / listings / client / gzipdecoder.py
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
6
7 class BeginningPrinter(Protocol):
8     def __init__(self, finished):
9         self.finished = finished
10         self.remaining = 1024 * 10
11
12
13     def dataReceived(self, bytes):
14         if self.remaining:
15             display = bytes[:self.remaining]
16             print 'Some data received:'
17             print display
18             self.remaining -= len(display)
19
20
21     def connectionLost(self, reason):
22         print 'Finished receiving body:', reason.type, reason.value
23         self.finished.callback(None)
24
25
26
27 def printBody(response):
28     finished = Deferred()
29     response.deliverBody(BeginningPrinter(finished))
30     return finished
31
32
33 def main():
34     agent = ContentDecoderAgent(Agent(reactor), [('gzip', GzipDecoder)])
35
36     d = agent.request('GET', 'http://www.yahoo.com/')
37     d.addCallback(printBody)
38     d.addErrback(log.err)
39     d.addCallback(lambda ignored: reactor.stop())
40     reactor.run()
41
42 if __name__ == "__main__":
43     main()