Skip to content

Instantly share code, notes, and snippets.

@wojons
Forked from schlamar/gist:2993700
Created April 6, 2014 05:45
Show Gist options
  • Select an option

  • Save wojons/10001962 to your computer and use it in GitHub Desktop.

Select an option

Save wojons/10001962 to your computer and use it in GitHub Desktop.

Revisions

  1. @schlamar schlamar revised this gist Feb 22, 2013. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -48,8 +48,7 @@ def http_class_wrapper(host, **kwargs):

    if __name__ == "__main__":
    handler = VerifiedHTTPSHandler(ca_certs=certifi.where())
    # assuming proxy settings are in environment
    # or set them with:
    # assuming proxy settings are in environment or set them with:
    # urllib2.ProxyHandler({'http_proxy': 'http://', 'https_proxy' = 'http://'})
    opener = urllib2.build_opener(handler, urllib2.ProxyHandler())
    opener.open('https://google.com').read()
  2. @schlamar schlamar revised this gist Feb 22, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -48,7 +48,9 @@ def http_class_wrapper(host, **kwargs):

    if __name__ == "__main__":
    handler = VerifiedHTTPSHandler(ca_certs=certifi.where())
    # assuming proxy settings are in environment (http_proxy and https_proxy)
    # assuming proxy settings are in environment
    # or set them with:
    # urllib2.ProxyHandler({'http_proxy': 'http://', 'https_proxy' = 'http://'})
    opener = urllib2.build_opener(handler, urllib2.ProxyHandler())
    opener.open('https://google.com').read()
    opener.open('https://kennethreitz.com').read()
  3. @schlamar schlamar revised this gist Feb 22, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -48,6 +48,7 @@ def http_class_wrapper(host, **kwargs):

    if __name__ == "__main__":
    handler = VerifiedHTTPSHandler(ca_certs=certifi.where())
    # assuming proxy settings are in environment (http_proxy and https_proxy)
    opener = urllib2.build_opener(handler, urllib2.ProxyHandler())
    opener.open('https://google.com').read()
    opener.open('https://kennethreitz.com').read()
  4. @schlamar schlamar revised this gist Jun 26, 2012. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ def connect(self):

    class VerifiedHTTPSHandler(urllib2.HTTPSHandler):
    def __init__(self, **kwargs):
    urllib2.AbstractHTTPHandler.__init__(self)
    urllib2.HTTPSHandler.__init__(self)
    self._connection_args = kwargs

    def https_open(self, req):
    @@ -45,8 +45,6 @@ def http_class_wrapper(host, **kwargs):

    return self.do_open(http_class_wrapper, req)

    https_request = urllib2.HTTPSHandler.do_request_


    if __name__ == "__main__":
    handler = VerifiedHTTPSHandler(ca_certs=certifi.where())
  5. @schlamar schlamar created this gist Jun 26, 2012.
    55 changes: 55 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    import httplib
    import urllib2
    import ssl

    import certifi
    from backports.ssl_match_hostname import match_hostname


    class CertValidatingHTTPSConnection(httplib.HTTPConnection):
    default_port = httplib.HTTPS_PORT

    def __init__(self, host, port=None, key_file=None, cert_file=None,
    ca_certs=None, strict=None, **kwargs):
    httplib.HTTPConnection.__init__(self, host, port, strict, **kwargs)
    self.key_file = key_file
    self.cert_file = cert_file
    self.ca_certs = ca_certs
    if self.ca_certs:
    self.cert_reqs = ssl.CERT_REQUIRED
    else:
    self.cert_reqs = ssl.CERT_NONE

    def connect(self):
    httplib.HTTPConnection.connect(self)
    self.sock = ssl.wrap_socket(self.sock, keyfile=self.key_file,
    certfile=self.cert_file,
    cert_reqs=self.cert_reqs,
    ca_certs=self.ca_certs)
    if self.cert_reqs & ssl.CERT_REQUIRED:
    cert = self.sock.getpeercert()
    hostname = self.host.split(':', 0)[0]
    match_hostname(cert, hostname)


    class VerifiedHTTPSHandler(urllib2.HTTPSHandler):
    def __init__(self, **kwargs):
    urllib2.AbstractHTTPHandler.__init__(self)
    self._connection_args = kwargs

    def https_open(self, req):
    def http_class_wrapper(host, **kwargs):
    full_kwargs = dict(self._connection_args)
    full_kwargs.update(kwargs)
    return CertValidatingHTTPSConnection(host, **full_kwargs)

    return self.do_open(http_class_wrapper, req)

    https_request = urllib2.HTTPSHandler.do_request_


    if __name__ == "__main__":
    handler = VerifiedHTTPSHandler(ca_certs=certifi.where())
    opener = urllib2.build_opener(handler, urllib2.ProxyHandler())
    opener.open('https://google.com').read()
    opener.open('https://kennethreitz.com').read()