Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
510 views
in Technique[技术] by (71.8m points)

Python Requests with wincertstore

I'm trying to connect to my corporate's internal webpages through the requests package, but since python does not use the windows default trusted certificates the connection is denied. I found out that wincertstore can be used to fetch the windows default certificates. But I'm still not sure how to use that along with the my request. Below is the code I have tried so far.............

import requests, socket, atexit, ssl, wincertstore
from requests.auth import HTTPBasicAuth
certfile = wincertstore.CertFile()
certfile.addstore("CA")
certfile.addstore("ROOT")
atexit.register(certfile.close)
ssl_sock = ssl.wrap_socket(s,ca_certs=certfile.name, 
cert_reqs=ssl.CERT_REQUIRED)
requests.get(url)

I get the following error................... requests.exceptions.SSLError: HTTPSConnectionPool(host='myhost', port=443): Max retries exceeded with url: myurl (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

I am able to use wget on the same url and download the content.

wget --no check certificate --user=my username --password=my password URL

But I am not interested in downloading the content as I only need to scrape a small portion of the webpage content.

Pythin version = 3.6.5

Wincertstore link - Link

Thanks in advance for your help..............

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had a similar issue and fixed it using the python-certifi-win32 package:

pip install python-certifi-win32

now you can just use:

requests.get(url, verify=True)

and the certificate is checked using the Windows Certificate Store.

Edit: This only works if the certificate is installed in the Windows Certificate Store...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...