[英]Fix SSL Certify Error Using Python Requests
系统设置:
我编写的代码过去曾经有效,但是现在运行它时,出现以下错误:
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)>
在尝试使用请求访问API之前,我曾遇到过这样的问题,它不允许我通过HTTPS建立连接。 该错误与代码无关,因为它在6个月前就起作用了。 是什么原因造成的? 附有我的证书吗?
我的函数正在调用的代码是:
url = self.DUST_SERVICE_URL
request_payload = self._args_to_payload(coordinate, radius=radius)
response = self._request("POST", url, data=request_payload,
timeout=timeout)
xml_tree = utils.xml(response.text)
result = SingleDustResult(xml_tree, coordinate)
return commons.FileContainer(result.ext_detail_table(),
show_progress=show_progress)
该代码来自一个程序包; 我没有写这段代码。
编辑:下面的完整错误消息。
/Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/urllib/request.py", line 1317, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1016, in _send_output
self.send(msg)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 956, in send
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py", line 1392, in connect
server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 412, in wrap_socket
session=session
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 850, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py", line 1108, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1045)
如果这是SSL问题(这是我所怀疑的),我将如何重置为系统默认值? 除了通过Python Requests请求内容外,我从没有涉足SSL或连接,所以我不知道如何进行更改。
问题出在macOS上的Python 3.7。 包含在Python 3.7中的是README.rtf
,它声明以下内容:
Python 3.7的此变体包括自己的OpenSSL 1.1.0私有副本。 苹果公司提供的已弃用的OpenSSL库不再使用。 这意味着,由Python ssl模块不再将由“钥匙串访问”应用程序和安全性命令行实用程序管理的系统和用户钥匙串中的信任证书用作默认值。
在同一文件夹中包含一个命令,该命令安装了用于certifi
的Python 3.7使用的证书捆绑包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.