import sys
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from twisted.web.client import Agent
class ResourcePrinter( Protocol ):
def __init__( self , finished ):
self.finished = finished
def dataReceived( self , data ):
print data
def connectionLost( self , reason ):
self.finished.callback( None )
def printResource( response ): # callback to process the response object
finished = Deferred( )
response.deliverBody( ResourcePrinter( finished ) ) # Protocol to print data from Deferred as it is received
return finished
def printError( failure ): # errback to process an errors
print >> sys.stderr , failure
def stop( result ):
reactor.stop( )
agent = Agent( reactor )
d = agent.request( 'GET', sys.argv[1] ) # returns a Deferred that fires with a Response object
d.addCallbacks( printResource, printError )
d.addBoth( stop ) # stops the reactor after it has finished processing the Callback or the ErrBack
reactor.run()