我正在尝试通过请求包连接到我公司的内部网页,但由于 python 不使用 Windows 默认可信证书,因此连接被拒绝。我发现 wincertstore 可用于获取 Windows 默认证书。但我仍然不确定如何将它与我的请求一起使用。以下是我到目前为止尝试过的代码………………….
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)
我收到以下错误…………………. 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')],)",),))
我可以在同一网址上使用 wget 并下载内容。
wget --no check certificate --user=my username --password=my password URL
但我对下载内容不感兴趣,因为我只需要抓取网页内容的一小部分。
Python 版本 = 3.6.5
Wincertstore 链接 - 链接
在此先感谢您的帮助…………..
原文由 adimessi30 发布,翻译遵循 CC BY-SA 4.0 许可协议
我有一个类似的问题并使用 python-certifi-win32 包修复它(现在停止维护):
截至 2022 年(如 Briareos386 在评论中提到的)
原始答案(停止维护)
现在你可以使用:
并使用 Windows 证书存储检查证书。
注意: 这仅在证书安装在 Windows 证书存储中时有效…