{"id":975,"hash":"116d848b1794208403460d299a77a3e730a5ed33abd15ae36f3ac730edc82f6e","pattern":"aiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed (&#39;151.101.193.69&#39;, 443)]","full_message":"here is my code:\n\nimport asyncio\nfrom aiohttp import ClientSession\n\nasync def main():\n    url = \"https://stackoverflow.com/\"\n\n    async with ClientSession() as session:\n        async with session.get(url) as resp:\n            print(resp.status)\n\nasyncio.run(main())\n\nif I run it on my computer, everything works, but if I run it on pythonanywhere, I get this error:\n\nTraceback (most recent call last):\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py\", line 936, in _wrap_create_connection\n    return await self._loop.create_connection(*args, **kwargs)  # type: ignore  # noqa\n  File \"/usr/lib/python3.8/asyncio/base_events.py\", line 1017, in create_connection\n    raise exceptions[0]\n  File \"/usr/lib/python3.8/asyncio/base_events.py\", line 1002, in create_connection\n    sock = await self._connect_sock(\n  File \"/usr/lib/python3.8/asyncio/base_events.py\", line 916, in _connect_sock\n    await self.sock_connect(sock, address)\n  File \"/usr/lib/python3.8/asyncio/selector_events.py\", line 485, in sock_connect\n    return await fut\n  File \"/usr/lib/python3.8/asyncio/selector_events.py\", line 517, in _sock_connect_cb\n    raise OSError(err, f'Connect call failed {address}')\nConnectionRefusedError: [Errno 111] Connect call failed ('151.101.193.69', 443)\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"test_c.py\", line 39, in <module>\n    asyncio.run(main())\n  File \"/usr/lib/python3.8/asyncio/runners.py\", line 43, in run\n    return loop.run_until_complete(main)\n  File \"/usr/lib/python3.8/asyncio/base_events.py\", line 608, in run_until_complete\n    return future.result()\n  File \"test_c.py\", line 28, in main\n    async with session.get(url, timeout=30) as resp:  # , headers=headers\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py\", line 1012, in __aenter__\n    self._resp = await self._coro\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/client.py\", line 480, in _request\n    conn = await self._connector.connect(\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py\", line 523, in connect\n    proto = await self._create_connection(req, traces, timeout)\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py\", line 858, in _create_connection\n    _, proto = await self._create_direct_connection(\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py\", line 1004, in _create_direct_connection\n    raise last_exc\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py\", line 980, in _create_direct_connection\n    transp, proto = await self._wrap_create_connection(\n  File \"/home/0dminnimda/.local/lib/python3.8/site-packages/aiohttp/connector.py\", line 943, in _wrap_create_connection\n    raise client_error(req.connection_key, exc) from exc\naiohttp.client_exceptions.ClientConnectorError: Cannot connect to host stackoverflow.com:443 ssl:default [Connect call failed ('151.101.193.69', 443)]\nUnclosed client session\nclient_session: <aiohttp.client.ClientSession object at 0x7f25a71d1a90>\n\naiohttp on hosting:\n\nName: aiohttp\nVersion: 3.6.2\nSummary: Async http client/server framework (asyncio)\nHome-page: https://github.com/aio-libs/aiohttp\nAuthor: Nikolay Kim\nAuthor-email: fafhrd91@gmail.com\nLicense: Apache 2\nLocation: /home/0dminnimda/.local/lib/python3.8/site-packages\nRequires: chardet, async-timeout, multidict, yarl, attrs\nRequired-by: \n\naiohttp on my PC:\n\nName: aiohttp\nVersion: 3.6.2\nSummary: Async http client/server framework (asyncio)\nHome-page: https://github.com/aio-libs/aiohttp\nAuthor: Nikolay Kim\nAuthor-email: fafhrd91@gmail.com\nLicense: Apache 2\nLocation: c:\\users\\asus\\appdata\\roaming\\python\\python38\\site-packages\nRequires: async-timeout, attrs, chardet, yarl, multidict\nRequired-by: \n\nI am at a loss that it is not so? I am running both files using python3.8.\n\nI also tried other urls, they have the same problem\n\nDo I need to add any more details?","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"first solution\nReferring to the help from the forum, I added trust_env = True when creating the client and now everything works.\n\nExplanation:\nFree 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.\n\nLink to aiohttp documentation (look for a parameter called \"trust_env\")\n\nHere is the new code:\n\nimport asyncio\nfrom aiohttp import ClientSession\n\nasync def main():\n    url = \"https://stackoverflow.com/\"\n\n    async with ClientSession(trust_env=True) as session:\n        async with session.get(url) as resp:\n            print(resp.status)\n\nasyncio.run(main())\n\nsolution if the first didn't help you\nThe domain you are trying to access must be in whitelist, otherwise you may also get this error.\n\nIn this case you need to post a new topic on the pythonanywhere forum asking to add the domain to the whitelist.\n\nIf this is an api, then you will need to provide a link to the documentation for this api.","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/63347818/aiohttp-client-exceptions-clientconnectorerror-cannot-connect-to-host-stackover","votes":28,"created_at":"2026-04-19T04:52:07.350765+00:00","updated_at":"2026-04-19T04:52:07.350765+00:00"}