使用Python请求修复SSL验证错误

时间:2018-11-07 04:29:39

标签: python macos ssl python-requests ssl-certificate

系统设置程序:

  • macOS 10.14.1
  • Python 3.7.0
  • 点18.1

我写的代码过去可以用,但是现在运行时,出现以下错误:

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请求来请求内容外,我从没有涉足SSL或连接,所以我不知道自己将如何进行更改。

1 个答案:

答案 0 :(得分:0)

问题出在macOS上的Python 3.7。 Python 3.7中包含一个README.rtf,其中声明了以下内容:

  

Python 3.7的此变体包括自己的OpenSSL 1.1.0私有副本。苹果公司提供的已弃用的OpenSSL库不再使用。这意味着,Python ssl模块不再将由Keychain Access应用程序和安全命令行实用程序管理的系统和用户钥匙串中的信任证书用作默认值。

同一文件夹中包含一个命令,该命令安装了用于certifi的Python 3.7使用的证书包。