Skip to content

Instantly share code, notes, and snippets.

@foobarna
Last active June 2, 2018 01:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foobarna/19c132304e140bf5031c273f6dc27ece to your computer and use it in GitHub Desktop.
Save foobarna/19c132304e140bf5031c273f6dc27ece to your computer and use it in GitHub Desktop.
aiohttp stream download
from contextlib import closing
import asyncio
import aiohttp
f = open('path/to/file', 'wb')
url = 'http://speedtest.wdc01.softlayer.com/downloads/test100.zip'
chunk_size = 5 * 2**20 # MB
async def download_file(session, url, f):
async with session.get(url) as response:
data_to_read = True
while data_to_read:
data = bytearray()
red = 0
while red < chunk_size:
chunk = await response.content.read(chunk_size - red)
if not chunk:
data_to_read = False
break
data.extend(chunk)
red += len(chunk)
f.write(data)
loop = asyncio.get_event_loop()
with closing(aiohttp.ClientSession(loop=loop)) as session:
loop.run_until_complete(download_file(session, url, f))
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment