HTTPSConnectionPool: Max retries exceeded with URL (Caused by SSLError)

29.8k Views Asked by At

I am trying to make an HTTPS request to a URL using Python requests library and certifi library as follows:

import certifi
url = 'https://10.0.0.39:4455/api/cars'

response = requests.get(url, verify=certifi.where())

if response.status_code == 200:
    print(response.json())
else:
    print(f'Request failed with status code {response.status_code}: {response.text}')

However, I keep getting the following error message:

"requests.exceptions.SSLError: HTTPSConnectionPool(host='https://10.0.0.39/api/cars', port=4455): 
Max retries exceeded with url: /api/cars (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)'),))"

I have already imported the certifi module to verify the SSL/TLS certificate. I have also installed the certifi module using pip. However, I am still getting this error message. How can I resolve this issue? also, the flask api is running on my other computer(10.0.0.39) and have an ssl certificat active.

2

There are 2 best solutions below

0
4x0t On

If you add verify=False to your requests.get() call and the error went away, it is likely that the SSL certificate issued by the server is not trusted on your client. To fix this, you can either use a trusted certificate from a recognized certificate authority on the API end, or add the certificate authority from your API to your client. if the error stay, try these commands to update your certifi libs

pip install --upgrade certifi if older version of python3

python -m pip install --upgrade certifi if newer version of python3

This will ensure that your client has the latest version of the library, which includes a set of trusted root certificates that may be needed to verify SSL certificates.

0
NRJ On

Create a file called openssl_conf. Put the following lines in it -

openssl_conf = openssl_init

[openssl_init]
ssl_conf = ssl_sect

[ssl_sect]
system_default = system_default_sect

[system_default_sect]
Options = UnsafeLegacyRenegotiation

Then run your code like this - $ OPENSSL_CONF=/path/to/openssl.cnf python ~/my_code.py