aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed (‘151.101.193.69’, 443)]

first solution

Referring to the help from the forum, I added trust_env = True when creating the client and now everything works.

Explanation:
Free accounts on PythonAnywhere must use a proxy to connect to the public internet, but aiohttp, by default, does not connect to a proxy accessible from an environment variable.

Link to aiohttp documentation (look for a parameter called “trust_env”)

Here is the new code:

import asyncio
from aiohttp import ClientSession


async def main():
    url = "https://stackoverflow.com/"

    async with ClientSession(trust_env=True) as session:
        async with session.get(url) as resp:
            print(resp.status)

asyncio.run(main())

solution if the first didn’t help you

The domain you are trying to access must be in whitelist, otherwise you may also get this error.

In this case you need to post a new topic on the pythonanywhere forum asking to add the domain to the whitelist.
If this is an api, then you will need to provide a link to the documentation for this api.

Leave a Comment