[Python] Certificate validating HTTPSHandler class, Verifie →→→→→进入此内容的聊天室

来自 , 2019-09-30, 写在 Python, 查看 219 次.
URL http://www.code666.cn/view/9739efc4
  1. from httplib import HTTPSConnection
  2. import urllib2
  3. import socket
  4. import ssl
  5.  
  6. class VerifiedHTTPSConnection(HTTPSConnection):
  7.         '''
  8.         Modified version of the httplib.HTTPSConnection class that forces server
  9.         certificate validation
  10.         '''
  11.         def __init__(self, host, port=None, key_file=None, cert_file=None,
  12.                 strict=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None,
  13.                 ca_file=None):
  14.                         HTTPSConnection.__init__(self, host, port, key_file,
  15.                                 cert_file, strict, timeout, source_address)
  16.  
  17.                         self.ca_file = ca_file
  18.  
  19.         def connect(self):
  20.                 sock = socket.create_connection(
  21.                         (self.host, self.port),
  22.                         self.timeout, self.source_address
  23.                 )
  24.  
  25.                 if self._tunnel_host:
  26.                         self.sock = sock
  27.                         self._tunnel()
  28.  
  29.                 if (None != self.ca_file):
  30.                         # Wrap the socket using verification with the root certs, note the hardcoded path
  31.                         self.sock = ssl.wrap_socket(
  32.                                 sock,
  33.                                 self.key_file,
  34.                                 self.cert_file,
  35.                                 cert_reqs = ssl.CERT_REQUIRED, # NEW: Require certificate validation
  36.                                 ca_certs = self.ca_file # NEW: Path to trusted CA file
  37.                         )
  38.                 else:
  39.                         self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file)
  40.  
  41. class VerifiedHTTPSHandler(urllib2.HTTPSHandler):
  42.         '''
  43.         Modified version of the urllib2.HTTPSHandler class that uses the
  44.         VerifiedHTTPSConnection HTTPSConnection class
  45.         '''
  46.         def __init__(self, debuglevel=0, key_file=None, cert_file=None, ca_file=None):
  47.                 urllib2.HTTPSHandler.__init__(self, debuglevel)
  48.                 self.key_file = key_file
  49.                 self.cert_file = cert_file
  50.                 self.ca_file = ca_file
  51.  
  52.         def https_open(self, req):
  53.                 return self.do_open(self.get_connection, req)
  54.  
  55.         def get_connection(self, host, timeout = socket._GLOBAL_DEFAULT_TIMEOUT):
  56.                 return VerifiedHTTPSConnection(host, timeout = timeout, ca_file = self.ca_file)
  57.  
  58. def main():
  59.         # ca_file needs to be a PEM-formatted listing of certificate roots
  60.         # See http://curl.haxx.se/ca/cacert.pem for an example
  61.         # Or https://raw.github.com/Caligatio/nss-root-converter/master/nss-coverter-py27.py
  62.         opener = urllib2.build_opener(VerifiedHTTPSHandler(ca_file = 'cacert.pem'))
  63.         urllib2.install_opener(opener)
  64.         request = urllib2.Request('https://www.google.com')
  65.         sock = urllib2.urlopen(request)
  66.         data = sock.read()
  67.         print(data)
  68.  
  69. if ('__main__' == __name__):
  70.         main()
  71. #//python/4445

回复 "Certificate validating HTTPSHandler class, Verifie"

这儿你可以回复上面这条便签

captcha