import asyncio
import aiohttp
import aiofiles
async def download_image(url, filename):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
async with aiofiles.open(filename, 'wb') as f:
while True:
chunk = await response.content.read(1024)
if not chunk:
break
await f.write(chunk)
print(f"Downloaded {filename}")
async def main():
urls = [
"https://via.placeholder.com/150/0000FF/808080?Text=Digital.com",
"https://via.placeholder.com/150/FF0000/FFFFFF?Text=WebsiteBuilders.com",
"https://via.placeholder.com/150/FFFF00/000000?Text=HostingFacts.com"
]
tasks = []
for i, url in enumerate(urls):
filename = f"image_{i+1}.jpg"
task = asyncio.ensure_future(download_image(url, filename))
tasks.append(task)
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())