Python - 证书验证失败

Python - Certificate verify failed

我正在使用 python 创建电报机器人,它从 API 获取数据(我正在使用请求和 http.client)。它从昨天开始工作。现在我得到这个错误。我在 google 上搜索,发现它可能是我的一些证书。我遵循了一些我发现的答案,这些答案告诉我下载并安装一个新证书,但它没有用。这是我 运行 我的 .py 代码时得到的。您知道问题是什么以及我该如何解决吗?

Traceback (most recent call last):
  File "C:\Users652\Documents\Github\Telegram-bot\test.py", line 10, in <module>
    conn.request("GET", "/games?date=2019-11-23", headers=headers)
  File "C:\Users652\anaconda3\envs\telegram\lib\http\client.py", line 1253, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "C:\Users652\anaconda3\envs\telegram\lib\http\client.py", line 1299, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "C:\Users652\anaconda3\envs\telegram\lib\http\client.py", line 1248, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "C:\Users652\anaconda3\envs\telegram\lib\http\client.py", line 1008, in _send_output
    self.send(msg)
  File "C:\Users652\anaconda3\envs\telegram\lib\http\client.py", line 948, in send
    self.connect()
  File "C:\Users652\anaconda3\envs\telegram\lib\http\client.py", line 1422, in connect
    self.sock = self._context.wrap_socket(self.sock,
  File "C:\Users652\anaconda3\envs\telegram\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users652\anaconda3\envs\telegram\lib\ssl.py", line 1040, in _create
    self.do_handshake()
  File "C:\Users652\anaconda3\envs\telegram\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1123)

代码为运行ning :

import http.client

conn = http.client.HTTPSConnection("v1.basketball.api-sports.io")

headers = {
    'x-rapidapi-host': "v1.basketball.api-sports.io",
    'x-rapidapi-key': "XxXxXxXxXxXxXxXxXxXxXxXx"
    }

conn.request("GET", "/games?date=2019-11-23", headers=headers)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))

我想补充一点,这段代码 运行2 天前完全没问题。

如果证书是通过 Let's encrypt 颁发的,请确保在客户端的中间证书存储中删除过期的 DST Root CA X3 颁发的 R3 证书。

此外,如果您改用请求库,您也可以解决该问题 (https://pypi.org/project/requests/)。