<s> import urllib . request , urllib . error <EOL> import http . client , socket <EOL> from urllib . response import addinfourl <EOL> try : <EOL> from . import ntlm <EOL> except ValueError : <EOL> import ntlm <EOL> import re <EOL> class AbstractNtlmAuthHandler : <EOL> def __init__ ( self , password_mgr = None ) : <EOL> if password_mgr is None : <EOL> password_mgr = HTTPPasswordMgr ( ) <EOL> self . passwd = password_mgr <EOL> self . add_password = self . passwd . add_password <EOL> def http_error_authentication_required ( self , auth_header_field , req , fp , headers ) : <EOL> auth_header_value_list = headers . get_all ( auth_header_field ) <EOL> if auth_header_value_list : <EOL> if any ( [ hv . lower ( ) == '<STR_LIT>' for hv in auth_header_value_list ] ) : <EOL> fp . close ( ) <EOL> return self . retry_using_http_NTLM_auth ( req , auth_header_field , None , headers ) <EOL> def retry_using_http_NTLM_auth ( self , req , auth_header_field , realm , headers ) : <EOL> user , pw = self . passwd . find_user_password ( realm , req . get_full_url ( ) ) <EOL> if pw is not None : <EOL> user_parts = user . split ( '<STR_LIT:\\>' , <NUM_LIT:1> ) <EOL> if len ( user_parts ) == <NUM_LIT:1> : <EOL> UserName = user_parts [ <NUM_LIT:0> ] <EOL> DomainName = '<STR_LIT>' <EOL> type1_flags = ntlm . NTLM_TYPE1_FLAGS & ~ ntlm . NTLM_NegotiateOemDomainSupplied <EOL> else : <EOL> DomainName = user_parts [ <NUM_LIT:0> ] . upper ( ) <EOL> UserName = user_parts [ <NUM_LIT:1> ] <EOL> type1_flags = ntlm . NTLM_TYPE1_FLAGS <EOL> headers = dict ( req . headers ) <EOL> headers . update ( req . unredirected_hdrs ) <EOL> auth = '<STR_LIT>' % ntlm . create_NTLM_NEGOTIATE_MESSAGE ( user , type1_flags ) <EOL> if req . headers . get ( self . auth_header , None ) == auth : <EOL> return None <EOL> headers [ self . auth_header ] = auth <EOL> host = req . host <EOL> if not host : <EOL> raise urllib . request . URLError ( '<STR_LIT>' ) <EOL> h = None <EOL> if req . get_full_url ( ) . startswith ( '<STR_LIT>' ) : <EOL> h = http . client . HTTPSConnection ( host ) <EOL> else : <EOL> h = http . client . HTTPConnection ( host ) <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> headers = dict ( ( name . title ( ) , val ) for name , val in list ( headers . items ( ) ) ) <EOL> h . request ( req . get_method ( ) , req . selector , req . data , headers ) <EOL> r = h . getresponse ( ) <EOL> r . begin ( ) <EOL> r . _safe_read ( int ( r . getheader ( '<STR_LIT>' ) ) ) <EOL> try : <EOL> if r . getheader ( '<STR_LIT>' ) : <EOL> headers [ '<STR_LIT>' ] = r . getheader ( '<STR_LIT>' ) <EOL> except TypeError : <EOL> pass <EOL> r . fp = None <EOL> auth_header_value = r . getheader ( auth_header_field , None ) <EOL> m = re . match ( '<STR_LIT>' , auth_header_value ) <EOL> if m : <EOL> auth_header_value , = m . groups ( ) <EOL> ( ServerChallenge , NegotiateFlags ) = ntlm . parse_NTLM_CHALLENGE_MESSAGE ( auth_header_value [ <NUM_LIT:5> : ] ) <EOL> auth = '<STR_LIT>' % ntlm . create_NTLM_AUTHENTICATE_MESSAGE ( ServerChallenge , UserName , DomainName , pw , NegotiateFlags ) <EOL> headers [ self . auth_header ] = auth <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> headers = dict ( ( name . title ( ) , val ) for name , val in list ( headers . items ( ) ) ) <EOL> try : <EOL> h . request ( req . get_method ( ) , req . selector , req . data , headers ) <EOL> response = h . getresponse ( ) <EOL> def notimplemented ( ) : <EOL> raise NotImplementedError <EOL> response . readline = notimplemented <EOL> return addinfourl ( response , response . msg , req . get_full_url ( ) , response . code ) <EOL> except socket . error as err : <EOL> raise urllib . request . URLError ( err ) <EOL> else : <EOL> return None <EOL> class HTTPNtlmAuthHandler ( AbstractNtlmAuthHandler , urllib . request . BaseHandler ) : <EOL> auth_header = '<STR_LIT>' <EOL> def http_error_401 ( self , req , fp , code , msg , headers ) : <EOL> return self . http_error_authentication_required ( '<STR_LIT>' , req , fp , headers ) <EOL> class ProxyNtlmAuthHandler ( AbstractNtlmAuthHandler , urllib . request . BaseHandler ) : <EOL> """<STR_LIT>""" <EOL> auth_header = '<STR_LIT>' <EOL> def http_error_407 ( self , req , fp , code , msg , headers ) : <EOL> return self . http_error_authentication_required ( '<STR_LIT>' , req , fp , headers ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> url = "<STR_LIT>" <EOL> user = "<STR_LIT>" <EOL> password = "<STR_LIT>" <EOL> passman = urllib . request . HTTPPasswordMgrWithDefaultRealm ( ) <EOL> passman . add_password ( None , url , user , password ) <EOL> auth_basic = urllib . request . HTTPBasicAuthHandler ( passman ) <EOL> auth_digest = urllib . request . HTTPDigestAuthHandler ( passman ) <EOL> auth_NTLM = HTTPNtlmAuthHandler ( passman ) <EOL> proxy_handler = urllib . request . ProxyHandler ( { } ) <EOL> opener = urllib . request . build_opener ( proxy_handler , auth_NTLM ) <EOL> urllib . request . install_opener ( opener ) <EOL> response = urllib . request . urlopen ( url ) <EOL> print ( ( response . read ( ) ) ) </s>
