如何忽略python中asyncio的SL错误?

时间:2018-11-25 21:03:35

标签: python asynchronous python-asyncio

所以我的代码如下:

resources\views\vendor\backpack\crud\inc\datatables_logic.blade.php

有时会引发SL错误:

tasks = [asyncio.ensure_future(self.run(proxy)) for proxy in proxy_list]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))
res = [task.result() for task in tasks if task.result()]


async def run(self, proxy):
    async with aiohttp.ClientSession() as session:
        try:
            async with session.get(url='https://www.google.com', proxy=proxy,
                                   headers=self.get_headers(), timeout=5) as resp:
                return proxy
        except Exception as e:
            return None

因此,我想忽略此错误消息,以使其不会被打印。但似乎不可能吗?我什至尝试尝试/除整个任务和循环块外,但没有成功。它返回“ AttributeError:'_ GeneratorContextManager'对象没有属性'run_until_complete'”

我尝试了返工,因此代码看起来像:

SL error in data received
protocol: <asyncio.sslproto.SSLProtocol object at 0x04CBEFB0>
transport: <_SelectorSocketTransport fd=2020 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python\lib\asyncio\sslproto.py", line 526, in data_received
ssldata, appdata = self._sslpipe.feed_ssldata(data)
  File "C:\Program Files (x86)\Python\lib\asyncio\sslproto.py", line 207, in feed_ssldata
self._sslobj.unwrap()
  File "C:\Program Files (x86)\Python\lib\ssl.py", line 767, in unwrap
return self._sslobj.shutdown()
ssl.SSLError: [SSL: DECRYPTION_FAILED_OR_BAD_RECORD_MAC] decryption failed or bad record mac (_ssl.c:2592)

1 个答案:

答案 0 :(得分:0)

要完全忽略 SSL 证书错误,请在 aiohttp.ClientSession() 中使用 TCPConnector

这是一个示例代码,可帮助您走上正轨

import aiohttp
from aiohttp import TCPConnector
import asyncio

async def get(url):
    try:
        async with aiohttp.ClientSession(connector=TCPConnector(ssl=False)) as session:
            async with session.get(url, allow_redirects=True, headers={'User-Agent':'Chrome'}) as response:
                return url
    except Exception as E:
        pass

您仍然可以捕获异常并使用它。

Documentation 似乎不是最新的 ssl=False,因为它仍然将 verify_ssl=False 列为正确参数,但会抛出以下消息:

aiohttp_requests.py:7: DeprecationWarning: verify_ssl is deprecated, use ssl=False instead