aiohttp ssl校验的坑
| import aiohttp import asyncio
async def main(): async with aiohttp.ClientSession() as session: async with session.get('https://cube.meituan.com/topcube/api/toc/playWayPackage/sendCoupon?k=226281') as resp: print(await resp.read())
loop = asyncio.get_event_loop() loop.run_until_complete(main())
|
报错信息:
| aiohttp.client_exceptions.ClientConnectorCertificateError: Cannot connect to host cube.meituan.com:443 ssl:True [SSLCertVerificationError: (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997)')]
|
解决方法1:
| session.get(url, ssl=False)
|
解决方法2:
| async def main(): ssl_context = ssl.create_default_context(cafile=certifi.where()) conn = aiohttp.TCPConnector(ssl=ssl_context)
async with aiohttp.ClientSession(connector=conn) as session: async with session.get(url) as resp: print(await resp.read())
|
参考链接:
https://stackoverflow.com/questions/69605350/aiohttp-raises-an-certificate-error-with-some-sites-that-browser-opens-normally/69618487
https://stackoverflow.com/questions/70236730/ssl-sslcertverificationerror-ssl-certificate-verify-failed-certificate-verif
https://stackoverflow.com/questions/51925384/unable-to-get-local-issuer-certificate-when-using-requests-in-python
https://cloud.tencent.com/developer/article/1987854