Home > Software engineering >  Getting SSL: CERTIFICATE_VERIFY_FAILED when using proxy with python requests
Getting SSL: CERTIFICATE_VERIFY_FAILED when using proxy with python requests

Time:09-22

I'm trying to implement a proxy in python using the requests library but I keep getting the same error over and over again. This is my code:

proxies = {
        'http': 'http://127.0.0.1:24000',
        'https': 'https://127.0.0.1:24000',
    }
    resp = requests.get('https://api.myip.com', proxies=proxies)
    print(resp.text)

I am using Bright Data's proxy manager, and i suspect my implementation of the proxy is wrong. The error I'm getting is:

raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='api.myip.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)')))

I've tried solutions i found online like verify=false, which worked for this link but not for others I need to access, which is why I'm looking for a safer solution.

CodePudding user response:

If you have a copy of the self-signed certificate and key you can modify the code as follow:

proxies = {
    'http': 'http://127.0.0.1:24000',
    'https': 'http://127.0.0.1:24000',
}

certificate_path = os.path.join(CACERT_PATH, 'cacert.pem')
key_path = os.path.join(CACERT_KEY, 'cacert.key')

resp = requests.get('https://api.myip.com',
                    proxies=proxies,
                    cert=(certificate_path, key_path))
print(resp.text)

CodePudding user response:

verify=False is a one way of doing it, but a better way to disable those warnings is to use this:

import urllib3

urllib3.disable_warnings()
  • Related