<s> """<STR_LIT>""" <EOL> import sys <EOL> from pprint import pprint <EOL> from twisted import version <EOL> from twisted . python import log <EOL> from twisted . internet . defer import Deferred <EOL> from twisted . internet import reactor <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . web . iweb import UNKNOWN_LENGTH <EOL> from twisted . web . http_headers import Headers <EOL> from twisted . web . client import Agent , ResponseDone <EOL> class WriteToStdout ( Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . onConnLost = Deferred ( ) <EOL> def dataReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> print '<STR_LIT>' , data <EOL> def connectionLost ( self , reason ) : <EOL> if not reason . check ( ResponseDone ) : <EOL> reason . printTraceback ( ) <EOL> else : <EOL> print '<STR_LIT>' <EOL> self . onConnLost . callback ( None ) <EOL> def main ( reactor , url ) : <EOL> """<STR_LIT>""" <EOL> userAgent = '<STR_LIT>' % ( version . short ( ) , ) <EOL> agent = Agent ( reactor ) <EOL> d = agent . request ( <EOL> '<STR_LIT:GET>' , url , Headers ( { '<STR_LIT>' : [ userAgent ] } ) ) <EOL> def cbResponse ( response ) : <EOL> """<STR_LIT>""" <EOL> pprint ( vars ( response ) ) <EOL> proto = WriteToStdout ( ) <EOL> if response . length is not UNKNOWN_LENGTH : <EOL> print '<STR_LIT>' , response . length , '<STR_LIT>' <EOL> else : <EOL> print '<STR_LIT>' <EOL> response . deliverBody ( proto ) <EOL> return proto . onConnLost <EOL> d . addCallback ( cbResponse ) <EOL> d . addErrback ( log . err ) <EOL> d . addBoth ( lambda ign : reactor . callWhenRunning ( reactor . stop ) ) <EOL> reactor . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( reactor , * sys . argv [ <NUM_LIT:1> : ] ) </s>
