【发布时间】:2022-01-11 03:28:51
【问题描述】:
当我尝试将框架 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;
- 我在Bango 对question: 的建议下从网站www.python.org 获取的三个证书
对于这个冗长的问题,我深表歉意,但我在互联网上进行了很多搜索,但找不到适合我的案例的解决方案。 提前谢谢你们,伙计们
【问题讨论】:
-
试试
pip install certifi -
@salparadise 刚刚完成......不,它不起作用,它仍然给出相同的错误:(
-
不推荐长期使用,但是由于你本地的python找不到CA,你可以在脚本的那部分设置这个
session.get("https://python.org", verify=False)。 -
我做到了,现在它给了我
TypeError: ClientSession._request() got an unexpected keyword argument 'verify',也许参数以其他方式命名? -
啊抱歉,我认为这与
requests的参数相同
标签: python python-3.x ssl ssl-certificate aiohttp