SSL cert issue with Python Requests

2024/9/20 0:10:08

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 requestresp = self.send(prep, **send_kwargs)File "C:\Program Files\Python37-32\lib\site-packages\requests\sessions.py", line 646, in sendr = adapter.send(request, **kwargs)File "C:\Program Files\Python37-32\lib\site-packages\requests\adapters.py", line 514, in sendraise 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-warningsInsecureRequestWarning)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 requestresp = self.send(prep, **send_kwargs)File "C:\Program Files\Python37-32\lib\site-packages\requests\sessions.py", line 646, in sendr = adapter.send(request, **kwargs)File "C:\Program Files\Python37-32\lib\site-packages\requests\adapters.py", line 514, in sendraise 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?

Answer

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)
https://en.xdnf.cn/q/72746.html

Related Q&A

MatplotLib get all annotation by axes

im doing a project with Python and Tkinter. I can plot an array of data and i also implemented a function to add annotation on plot when i click with the mouse, but now i need a list of all annotation…

Using Pandas to applymap with access to index/column?

Whats the most effective way to solve the following pandas problem? Heres a simplified example with some data in a data frame: import pandas as pd import numpy as np df = pd.DataFrame(np.random.randin…

Multiple URL segment in Flask and other Python frameowrks

Im building an application in both Bottle and Flask to see which I am more comfortable with as Django is too much batteries included.I have read through the routing documentation of both, which is very…

installing python modules that require gcc on shared hosting with no gcc or root access

Im using Hostgator shared as a production environment and I had a problem installing some python modules, after using:pip install MySQL-pythonpip install pillowresults in:unable to execute gcc: Permiss…

Using libclang to parse in C++ in Python

After some research and a few questions, I ended up exploring libclang library in order to parse C++ source files in Python.Given a C++ source int fac(int n) {return (n>1) ? n∗fac(n−1) : 1; }for …

Python one class per module and packages

Im trying to structure my app in Python. Coming back from C#/Java background, I like the approach of one class per file. Id like my project tree to look like this:[Service][Database]DbClass1.pyDbClass2…

PyMySQL Access Denied using password (no) but using password

Headscratcher here for me.I am attempting to connect to a database on my local MySQL 8.0.11.0 install from Python.Heres the code Im using :conn = pymysql.connect(host=localhost, port=3306, user=root, p…

Trouble importing Python modules on Ninja IDE

I have been trying to import modules into Ninja IDE for python. These are modules that I have working on the terminal (numpy, scipy, scitools, matplotlib, and mpl_toolkits), but will not run correctly …

UTF-8 error with Python and gettext

I use UTF-8 in my editor, so all strings displayed here are UTF-8 in file.I have a python script like this:# -*- coding: utf-8 -*- ... parser = optparse.OptionParser(description=_(automates the dice ro…

Add build information in Jenkins using REST

Does anyone know how to add build information to an existing Jenkins build? What Im trying to do is replace the #1 build number with the actual full version number that the build represents. I can do …