Last active
June 2, 2018 01:10
-
-
Save foobarna/19c132304e140bf5031c273f6dc27ece to your computer and use it in GitHub Desktop.
aiohttp stream download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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