0

So I have a python script that needs to access webpage content via 'requests'. Due to the environment this script is running in, I need to use a virtual environment. However, this results in the request failing, since it cannot find the certificate from the virtual environment.

raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='...', port=443): Max retries exceeded with url: ... (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:992)')))

This issue does not occur when run outside the virtual environment, and I can see the error occur as a result of where requests it looking for certificates:

in venv (request fails):

>>> print (requests.certs.where())
/home/user/python/venv/lib64/python3.11/site-packages/certifi/cacert.pem

not in venv (request will certify successfully):

>>> print (requests.certs.where())
/etc/pki/tls/certs/ca-bundle.crt

I could specify the certification using request(url, verify=/path/to/cert) however this script needs to run on any machine, so hardcoding the path will not work.

I could ignore the verification, but this seems very dumb and bad.

Therefore I am wondering if there is a way to instruct python to use the same certification as the underlying environment rather than the venv path?

Is there some workaround where given a url, I could detect which certification will be used and provide that in my python script to the request?

Thank you

  • Specifying a ca-bundle should be able to be done on any machine via the `REQUESTS_CA_BUNDLE` environment variable. This way you can just specify exactly where the bundle is [Python Requests - How to use system ca-certificates (debian/ubuntu)?](https://stackoverflow.com/questions/42982143/python-requests-how-to-use-system-ca-certificates-debian-ubuntu) – C.Nivs Mar 03 '23 at 18:20
  • So you specify the ca bundle using that variable, but I guess my question is how do you detect the path to set this variable from on any given machine? – user21113865 Mar 03 '23 at 18:49
  • This could be alleviated via however you are deploying said code. If they are all *nix machines, you could install the bundle in the same location on each machine – C.Nivs Mar 03 '23 at 18:56

0 Answers0