python requests.exceptions.sslerror httpsconnectionpool

Dealing with Python Requests Exceptions SSL Error httpsconnectionpool

Recently, I encountered an error while using Python Requests module to send a GET request to an endpoint that required SSL certification. The error message was:

requests.exceptions.SSLError: HTTPSConnectionPool(host='example.com', port=443): Max retries exceeded with url: /api/v1/endpoint (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)')))

After researching and troubleshooting, I found out that this error occurs when the SSL certificate on the server hosting the endpoint is invalid or not trusted by the client making the request. Python Requests module is built on top of urllib3, which uses the certifi package to verify SSL certificates. Therefore, we can try to update the certifi package to the latest version to solve this issue. We can do this by running:

pip install --upgrade certifi

If updating certifi package does not solve the issue, we can try to disable SSL verification by passing the verify parameter to False when making the request. However, this is not recommended as it makes the request vulnerable to man-in-the-middle attacks. We can do this by adding:

requests.get('https://example.com/api/v1/endpoint', verify=False)

If none of the above solutions work, we may need to manually add the SSL certificate to our trusted certificates list. We can do this by downloading the certificate and adding it to the pem file used by the certifi package. More information on how to do this can be found in the certifi documentation.