Search code examples
pythonpython-2.7sslurlliburllib3

Python: Surpass SSL verification with urllib3


Connectiing to SSL server is throwing an error:

error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed

I'm using this urllib3 python library for connecting to server, using this method:

urllib3.connectionpool.connection_from_url(['remote_server_url'], maxsize=2)

How can i ignore SSL verification? Or is there another step for doing this?

Thanks.


Solution

  • You should be able to force urllib3 to use an unverified HTTPSConnection object by overriding the static ConnectionCls property of any ConnectionPool instance. For example:

    from urllib3.connection import UnverifiedHTTPSConnection
    from urllib3.connectionpool import connection_from_url
    
    # Get a ConnectionPool object, same as what you're doing in your question
    http = connection_from_url(remote_server_url, maxsize=2)
    
    # Override the connection class to force the unverified HTTPSConnection class
    http.ConnectionCls = UnverifiedHTTPSConnection
    
    # Make requests as normal...
    r = http.request(...)
    

    For additional reference, you can check some of our tests which do similar overriding.

    I've opened an issue to improve our documentation on how and when this should be done. We always appreciate pull requests, if you'd like to contribute. :)