4
votes

I'm making a request to a site which requires SSL cert to access. When I tried to access the URL, I get SSL Certificate error

import requests
proxies = {"https":"https://user:pwd@host:port"}
r = requests.get("https://URL", proxies=proxies)
print(r.status_code)

File "C:\Program Files\Python37-32\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "C:\Program Files\Python37-32\lib\site-packages\requests\sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "C:\Program Files\Python37-32\lib\site-packages\requests\adapters.py", line 514, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='pit-wvrpnpd.johnlewis.co.uk', port=443): Max retries exceeded with url: /suite-api (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1076)')))

If I use verify=False in Python's Requests, I'm getting below error

r = requests.get("https://URL", proxies=proxies,verify=False)

C:\Program Files\Python37-32\lib\site-packages\urllib3\connectionpool.py:851: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning) 403

So I decided to download the SSL certificate from Chrome "site.cer" Then I used it as below, yet it throws error

r = requests.get("https://URL", proxies=proxies, cert=('C:\\site.cer'))

File "C:\Program Files\Python37-32\lib\site-packages\requests\sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "C:\Program Files\Python37-32\lib\site-packages\requests\sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "C:\Program Files\Python37-32\lib\site-packages\requests\adapters.py", line 514, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='pit-wvrpnpd.johnlewis.co.uk', port=443): Max retries exceeded with url: /suite-api (Caused by SSLError(SSLError(9, '[SSL] PEM lib (_ssl.c:3854)')))

I'm using Python 3.7, any Idea where I made a mistake?

1
Note that what you got with verify=False is not an error. Is just a warning telling you that you probably don't want to use verify=False. The issue is that the website is using a self-signed certificate produced by a CA that you don't trust. Note that the option to use for server certificate validation is verify. The cert option is used to provide a client certificate (which is used to authenticate the client to the server, but your error is the client not verified the server). - Giacomo Alzetta
Something wrong with certificate. But cert param used for self signing requests, it won't solve your problem. Take a look on verify. - Olvin Roght
Note that after disabling server cert verification, you got a 403 status code. The server denied your request. That has nothing to do with TLS/SSL. - glibdud
Any Idea what's the next step to take on this? - Sujin Sam Arul Raj
The URL works fine in browser, but fails in code - Sujin Sam Arul Raj

1 Answers

4
votes

Adding this header with verify=False gave me access.

headers = {
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36",
}

response = requests.request("GET", url, headers=headers, verify=False)