Search code examples
pythonparametersssl-certificatehttplib2

Parameters of httplib2.Http.request()


I was wondering what parameters does the request method of the httplib2.Http class take. I'm trying to use a simple get method but it seems like the url I use provides a certificate that needs verification since I get an SSL3_GET_SERVER_CERTIFICATE error. That's why I was wondering if with the httplib2 library we could tell our request to ignore the certificate verification (like the unverifiable parameter of urllib.request). Here's my code :

   import httplib2
   h = httplib2.Http(".cache")
   h.credentials_add(username, password)
   resp, content = h.request(url, "GET")

Solution

  • You can use disable_ssl_certificate_validation to ignore certificate verification. Please check the sample below. However, it won't work with Python 3.x due to the following bug. So you need to update Python33\Lib\site-packages\httplib2\__init__.py as recommended in comments for this issue.

    HTTPS request doesn't work when disable_ssl_certificate_validation = True

    import httplib2
    
    h = httplib2.Http(".cache", disable_ssl_certificate_validation=True)
    resp, content = h.request("https://github.com/", "GET")
    

    Patch for Python 3.x:

    diff --git a/__init__.py b/__init__.py
    index 65f90ac..4495994 100644
    --- a/__init__.py
    +++ b/__init__.py
    @@ -823,10 +823,13 @@ class HTTPSConnectionWithTimeout(http.client.HTTPSConnection):
                     context.load_cert_chain(cert_file, key_file)
                 if ca_certs:
                     context.load_verify_locations(ca_certs)
    +        check_hostname = True
    +        if disable_ssl_certificate_validation:
    +            check_hostname = False
             http.client.HTTPSConnection.__init__(
                     self, host, port=port, key_file=key_file,
                     cert_file=cert_file, timeout=timeout, context=context,
    -                check_hostname=True)
    +                check_hostname=check_hostname)
    
    
     SCHEME_TO_CONNECTION = {