Question is regarding aiohttp
libriary usage.
My goal here is to check list of urls by sending bunch of HEAD
requests, potentially asynchronously ,and eventually create dict of
url: status pairs
.
I am new in asyncio
and stuff and I found a lot of examples where people use GET
requests to fetch html ,for example ,and they use await resp.read()
or await resp.text()
and it works fine but with HEAD
request I don’t have body, I just have header, that's it. If I try to await resp.status
or resp
itself as an object – it does not work as they are not awaitable.
Code below works only synchronously step by step and I can’t figure out how to make it run asynchronously. Seems like whatever i do with status turns code to sync mode somehow...
I would be glad to see your ideas.
Thanks.
import asyncio
import aiohttp
urls_list = [url1, url2, url3, etc, etc, etc, ]
status_dict = {}
async def main():
async with aiohttp.ClientSession() as session:
for individual_url in urls_list:
async with session.head(individual_url) as resp:
status_dict.update({url: resp.status})
asyncio.run(main())