1

so my code looks like:

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

Sometimes it throws the SL error:

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)

So I want to ignore this error message so it won't be printed. but it seems it's not possible? I even tried to try/except the whole task and loop block, but it did not work. It gives back "AttributeError: '_GeneratorContextManager' object has no attribute 'run_until_complete'"

I tried the rework, so the code looks :

@contextlib.contextmanager
def suppress_ssl_exception_report(self):
    loop = asyncio.get_event_loop()
    old_handler = loop.get_exception_handler()
    old_handler_fn = old_handler or (lambda _loop, ctx: loop.default_exception_handler(ctx))

    def ignore_exc(_loop, ctx):
        exc = ctx.get('exception')
        if isinstance(exc, SSLCertVerificationError):
            return
        old_handler_fn(loop, ctx)

    loop.set_exception_handler(ignore_exc)
    try:
        yield
    finally:
        loop.set_exception_handler(old_handler)
    return loop


tasks = [asyncio.ensure_future(self.run(proxy)) for proxy in proxy_list]
loop = self.suppress_ssl_exception_report()
loop.run_until_complete(asyncio.wait(tasks))
res = [task.result() for task in tasks if task.result()]
bot1
  • 77
  • 2
  • 12
  • 1
    You could try the approach given in [this answer](https://stackoverflow.com/a/52021212/1600898). – user4815162342 Nov 26 '18 at 07:42
  • @user4815162342 I tried to do some re-work on it. but there is some problem, maybe has something to do with python3? – bot1 Nov 27 '18 at 14:31
  • so I am bit confused, I thought loop is a object returned by get_event_loop – bot1 Nov 27 '18 at 14:34
  • You're not supposed to assign the result of `suppress_ssl_exception_report` to `loop`. Instead, you should use it in a `with` statement around the part of code that raises the exception you can't suppress. Also, you should change `SSLCertVerificationError` to a more generic exception type. – user4815162342 Nov 27 '18 at 14:35

1 Answers1

1

To fully ignore the SSL Certificate error, use the TCPConnector in the aiohttp.ClientSession().

Here is a sample code to help you get on the track

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

You can still catch the Exception and work with it.

The Documentation seems not up to date with this ssl=False, as it is still listing the verify_ssl=False to be a correct argument, but will throw the following message:

aiohttp_requests.py:7: DeprecationWarning: verify_ssl is deprecated, use ssl=False instead
Nioooooo
  • 409
  • 4
  • 5