gpt4 book ai didi

python - ssl.SSLCertVerificationError : [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl. c:997)

转载 作者:行者123 更新时间:2023-12-04 22:35:17 25 4
gpt4 key购买 nike

当我尝试使用框架 aiohhtp 时,我正在玩一些 Python 的 Web 框架。使用此代码(取自文档):

import aiohttp
import asyncio

#********************************
# a solution I found on the forum:
# https://stackoverflow.com/questions/50236117/scraping-ssl-certificate-verify-failed-error-for-http-en-wikipedia-org?rq=1
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
# ... but it doesn't work :(
#********************************

async def main():

async with aiohttp.ClientSession() as session:
async with session.get("https://python.org") as response:

print("Status:", response.status)
print("Content-type:", response.headers["content-type"])

html = await response.text()
print("Body:", html[:15], "...")

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
当我运行这段代码时,我得到了这个回溯:
DeprecationWarning: There is 
no current event loop
loop = asyncio.get_event_loop()
Traceback (most recent call last):
File "c:\Python310\lib\site-packages\aiohttp\connector.py", line 986, in _wrap_create_connection
return await self._loop.create_connection(*args, **kwargs) # type: ignore[return-value] # noqa
File "c:\Python310\lib\asyncio\base_events.py", line 1080, in create_connection
transport, protocol = await self._create_connection_transport(
File "c:\Python310\lib\asyncio\base_events.py", line 1110, in _create_connection_transport
await waiter
File "c:\Python310\lib\asyncio\sslproto.py", line 528, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
File "c:\Python310\lib\asyncio\sslproto.py", line 188, in feed_ssldata
self._sslobj.do_handshake()
File "c:\Python310\lib\ssl.py", line 974, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "c:\Users\chris\Documents\Programmi_in_Python_offline\Esercitazioni\Python_commands\aioWebTest.py", line 21, in <module>
loop.run_until_complete(main())
File "c:\Python310\lib\asyncio\base_events.py", line 641, in run_until_complete
return future.result()
File "c:\Users\chris\Documents\Programmi_in_Python_offline\Esercitazioni\Python_commands\aioWebTest.py", line 12, in main
async with session.get("https://python.org") as response:
File "c:\Python310\lib\site-packages\aiohttp\client.py", line 1138, in __aenter__
self._resp = await self._coro
File "c:\Python310\lib\site-packages\aiohttp\client.py", line 535, in _request
conn = await self._connector.connect(
File "c:\Python310\lib\site-packages\aiohttp\connector.py", line 542, in connect
proto = await self._create_connection(req, traces, timeout)
File "c:\Python310\lib\site-packages\aiohttp\connector.py", line 907, in _create_connection
_, proto = await self._create_direct_connection(req, traces, timeout)
File "c:\Python310\lib\site-packages\aiohttp\connector.py", line 1206, in _create_direct_connection
raise last_exc
File "c:\Python310\lib\site-packages\aiohttp\connector.py", line 1175, in _create_direct_connection
transp, proto = await self._wrap_create_connection(
File "c:\Python310\lib\site-packages\aiohttp\connector.py", line 988, in _wrap_create_connection
raise ClientConnectorCertificateError(req.connection_key, exc) from exc
aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host python.org:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:997)')]
从最后一行我认为这是证书过期的问题,所以我在互联网上搜索并尝试解决安装一些证书:
  • COMODO ECC Certification Authority ;
  • 我从网站 www.python.org 获取的三个证书在 Bango 下对 question 的建议:

  • 对于这个冗长的问题,我很抱歉,但我在互联网上进行了很多搜索,但找不到适合我的案例的解决方案。
    提前谢谢你们,伙计们<3

    最佳答案

    接受@salparadise 的评论,以下内容对我有用:

    session.get("https://python.org", ssl=False)

    关于python - ssl.SSLCertVerificationError : [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl. c:997),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70236730/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com