#NEWLINE# def match_hostname( cert , hostname ) : #NEWLINE# #INDENT# 'Verify that *cert* (in decoded format as returned by\n    SSLSocket.getpeercert()) matches the *hostname*.  RFC 2818 and RFC 6125\n    rules are followed, but IP addresses are not accepted for *hostname*.\n\n    CertificateError is raised on failure. On success, the function\n    returns nothing.\n    ' #NEWLINE# if ( not cert ) : #NEWLINE# #INDENT# raise ValueError ( 'empty or no certificate' ) #NEWLINE# #UNINDENT# dnsnames = [ ] #NEWLINE# san = cert . get ( 'subjectAltName' , ( ) ) #NEWLINE# for ( key , value ) in san : #NEWLINE# #INDENT# if ( key == 'DNS' ) : #NEWLINE# #INDENT# if _dnsname_match ( value , hostname ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# dnsnames . append ( key ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not dnsnames ) : #NEWLINE# #INDENT# for sub in cert . get ( 'subject' , ( ) ) : #NEWLINE# #INDENT# for ( key , value ) in sub : #NEWLINE# #INDENT# if ( key == 'commonName' ) : #NEWLINE# #INDENT# if _dnsname_match ( value , hostname ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# dnsnames . append ( value ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( len ( dnsnames ) > 1 ) : #NEWLINE# #INDENT# raise CertificateError ( ( "hostname %r doesn't match either of %s" % ( hostname , ', ' . join ( map ( repr , dnsnames ) ) ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( dnsnames ) == 1 ) : #NEWLINE# #INDENT# raise CertificateError ( ( "hostname %r doesn't match %r" % ( hostname , dnsnames [ 0 ] ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise CertificateError ( 'no appropriate commonName or subjectAltName fields were found' )