john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

download url agent

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()

  • « download url
  • download url headers »

Published

May 26, 2013

Category

python-twisted

~116 words

Tags

  • agent 2
  • download 12
  • python 180
  • twisted 20
  • url 14