#NEWLINE# def openURL( url , headers = None , method = 'GET' , body = None , host = '127.0.0.1' , port = 8000 , http_conn = HTTPConnection , protocol = 'HTTP/1.1' ) : #NEWLINE# #INDENT# 'Open the given HTTP resource and return status, headers, and body.' #NEWLINE# headers = cleanHeaders ( headers , method , body , host , port ) #NEWLINE# for trial in range ( 10 ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# if hasattr ( http_conn , 'host' ) : #NEWLINE# #INDENT# conn = http_conn #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# conn = http_conn ( interface ( host ) , port ) #NEWLINE# #UNINDENT# conn . _http_vsn_str = protocol #NEWLINE# conn . _http_vsn = int ( '' . join ( [ x for x in protocol if x . isdigit ( ) ] ) ) #NEWLINE# if ( sys . version_info < ( 2 , 4 ) ) : #NEWLINE# #INDENT# def putheader( self , header , value ) : #NEWLINE# #INDENT# if ( ( header == 'Accept-Encoding' ) and ( value == 'identity' ) ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# self . __class__ . putheader ( self , header , value ) #NEWLINE# #UNINDENT# import new #NEWLINE# conn . putheader = new . instancemethod ( putheader , conn , conn . __class__ ) #NEWLINE# conn . putrequest ( method . upper ( ) , url , skip_host = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not py3k ) : #NEWLINE# #INDENT# conn . putrequest ( method . upper ( ) , url , skip_host = True , skip_accept_encoding = True ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# import http.client #NEWLINE# def putrequest( self , method , url ) : #NEWLINE# #INDENT# if ( self . _HTTPConnection__response and self . _HTTPConnection__response . isclosed ( ) ) : #NEWLINE# #INDENT# self . _HTTPConnection__response = None #NEWLINE# #UNINDENT# if ( self . _HTTPConnection__state == http . client . _CS_IDLE ) : #NEWLINE# #INDENT# self . _HTTPConnection__state = http . client . _CS_REQ_STARTED #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise http . client . CannotSendRequest ( ) #NEWLINE# #UNINDENT# self . _method = method #NEWLINE# if ( not url ) : #NEWLINE# #INDENT# url = ntob ( '/' ) #NEWLINE# #UNINDENT# request = ntob ( ' ' ) . join ( ( method . encode ( 'ASCII' ) , url , self . _http_vsn_str . encode ( 'ASCII' ) ) ) #NEWLINE# self . _output ( request ) #NEWLINE# #UNINDENT# import types #NEWLINE# conn . putrequest = types . MethodType ( putrequest , conn ) #NEWLINE# conn . putrequest ( method . upper ( ) , url ) #NEWLINE# #UNINDENT# #UNINDENT# for ( key , value ) in headers : #NEWLINE# #INDENT# conn . putheader ( key , ntob ( value , 'Latin-1' ) ) #NEWLINE# #UNINDENT# conn . endheaders ( ) #NEWLINE# if ( body is not None ) : #NEWLINE# #INDENT# conn . send ( body ) #NEWLINE# #UNINDENT# response = conn . getresponse ( ) #NEWLINE# ( s , h , b ) = shb ( response ) #NEWLINE# if ( not hasattr ( http_conn , 'host' ) ) : #NEWLINE# #INDENT# conn . close ( ) #NEWLINE# #UNINDENT# return ( s , h , b ) #NEWLINE# #UNINDENT# except socket . error : #NEWLINE# #INDENT# time . sleep ( 0.5 ) #NEWLINE# if ( trial == 9 ) : #NEWLINE# #INDENT# raise