Fixing aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host :443 ssl :default Connect call failed

Aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host :443 ssl :default Connect call failed is an error which occurs when aiohttp fails because of an SSL problem or because of the PythonAnywhere proxy.

Today I try to explain why this error takes place and how to solve it, I will also add other solutions that could solve the error if possible.

Exploring the Error : aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host :443 ssl :default Connect call failed

This is an error which occurs when aiohttp fails because of an SSL problem or because of the PythonAnywhere proxy.

Please double check so you can avoid mixing between errors. The error message should look like the error message bellow.

                                                                       #
aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host somewebsite.com:443 ssl:default [Connect call failed ('', 443)]
Unclosed client session
client_session: <aiohttp.client.ClientSession object at  ...
                                                                       #

Bellow is a number of tested solutions that I have tried and worked for me.

Solution 1 : use the class asyncio.set_event_loop_policy() or avoid SSL check, using ssl=False

In the first method, we will deal with the SSL probability, assuming that aiohttp fails because of an SSL problem.

We have two options to solve this issue, the first option is the undesirable option. Which is to ignore the SSL verification completely. This will expose you to multiple vulnerabilities like man in the middle attacks for example.

If you are okay with the consequences, then start by importing aiohttp and then handling the connector.

                                                                       #
import aiohttp
myconnector = aiohttp.TCPConnector()
                                                                       #

In this example we will work with twitter and disactivate SSL, using ssl=False

                                                                       #
async with aiohttp.ClientSession(connector=myconnector) as session:
    await session.get('https://twitter.com', ssl=False)
                                                                       #

The second solution is to use the class asyncio.set_event_loop_policy() like this.

                                                                       #
mypolicy = asyncio.WindowsSelectorEventLoopPolicy()
asyncio.set_event_loop_policy(mypolicy)
                                                                       #

If these two options did not solve the issue then the issue is PythonAnywhere. The method bellow will most likely solve that problem.

Solution 2 : add trust_env = True when using PythonAnywhere

PythonAnywhere has paid and free accounts, if you have the free option you must use a proxy which in turn causes this problem.

Since PythonAnywhere has a proxy accessible from an env. This causes the error since anaiohttp cannot connect to these types of proxies.

The solution is to add trust_env = True to your code, just like this.

                                                                       #
async with ClientSession(trust_env=True) as session
                                                                       #

Instead of doing this when creatng the client

                                                                       #
async with ClientSession() as session:
                                                                       #

I hope the methods and options I have provided helped you solve the issue.

Summing-up : 

That is it guys, this is the end of this article aka guide, I hope you found it useful in solving the error : aiohttp.client_exceptions.ClientConnectorError Cannot connect to host 443 ssl default Connect call failed , make sure to support our work on Kofi, you do not have to but hey you can donate to the team.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/