{"id":992,"hash":"9534da0c80322ccae964cb1d560aa0d0da2f5f790e72cadaea11007fcbe0648d","pattern":"Asynchronous HTTP calls using aiohttp/asyncio fail with &quot;Cannot connect to host [Network is unreachable]&quot;","full_message":"I am implementing a fast async REST interface caller with which I want to upload data in an synchronous manner. For now I am about to build the async framework that just calls the python page in an async manner and measures the delay. \n\nHere is the code (which does not work as described below):\n\nimport aiohttp\nimport asyncio\nimport async_timeout\nfrom timeit import default_timer as timer\n\nasync def fetch(session, url):\n    start = timer()\n    with async_timeout.timeout(10):\n        async with session.get(url) as response:\n            date = response.headers.get(\"DATE\")\n            end = timer()\n            delay = end - start\n            print(\"{}:{} with delay {} s\".format(date, response.url, delay))\n            return await response.read()\n\nasync def bound_call(semaphore, session, url):\n    async with semaphore:\n        await fetch(session, url)\n\nasync def perform_call(session):\n    sem = asyncio.Semaphore(1000)\n    html = await bound_call(sem, session, 'http://python.org')\n\nasync def perform_calls(n):\n    tasks = []\n    async with aiohttp.ClientSession() as session:\n        for i in range(n):\n            task = perform_call(session)\n            tasks.append(task)\n\n        responses = asyncio.gather(*tasks)\n        await responses\n\ncall_number = 10\n\nloop = asyncio.get_event_loop()\nloop.run_until_complete(perform_calls(call_number))\n\nIt properly works as long as I am using \n\nawait perform_call(session)\n\nbut that obviously breaks asynchonous calls. If I replace it with \n\n            task = perform_call(session)\n            tasks.append(task)\n\n        responses = asyncio.gather(*tasks)\n        await responses\n\nto make it await all responses at the same time, I get the error below:\n\naiohttp.client_exceptions.ClientConnectorError: Cannot connect to host python.org:80 ssl:False [Network is unreachable]\n\nI tried to run this as code from a jupyter notebook, and, since it did not run, copied it into normal code. In both cases, it ran on python 3.5. Unfortunately, I could not find a solution to the problem. It seems that there is no network access as soon as I try to use gather. Does anybody have a suggestion why it does not work? I am happy for any suggestions.","ecosystem":"pypi","package_name":"python-3.x","package_version":null,"solution":"Ok, I found the answer in a question my question is a duplicate of (I only found the duplicate after I posted here.)\n\nThe solution is just to initialize the client session using:\n\nimport socket # together with your other imports\n\nconn = aiohttp.TCPConnector(\n        family=socket.AF_INET,\n        verify_ssl=False,\n    )\n\n   # Create client session that will ensure we dont open new connection\n   # per each request.\n   async with aiohttp.ClientSession(connector=conn) as session:\n\nExplanation based on the duplicate:\nThe client session will use the connection instead of the default AsyncResolver as the resolver for the connection. It used to be the default resolver. The problem seems to be related to domains with ipv6 where the AsyncResolver has problems, so the solution is to simply specify the family to ipv4 addresses which is what we do with family=socket.AF_INET.\n\nDuplicate of: python 3.5 asyncio and aiohttp Errno 101 Network is unreachable","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/48007453/asynchronous-http-calls-using-aiohttp-asyncio-fail-with-cannot-connect-to-host","votes":10,"created_at":"2026-04-19T04:52:07.361098+00:00","updated_at":"2026-04-19T04:52:07.361098+00:00"}