from pathlib import Path
import aiofiles
import asyncio
import aiohttp
root_dir = Path('download')
host_url = "https://xxxxxx/zzz{}"
async def download_file(session, url, save_path):
async with session.get(url, ssl=False) as response:
if response.status == 200:
async with aiofiles.open(save_path, 'wb') as f:
await f.write(await response.read())
async def main():
urls = []
save_paths = []
for n in range(2012):
n_name = str(n).rjust(6, '0') + '.ts'
url = host_url.format(n_name)
save_path = root_dir.joinpath(n_name)
urls.append(url)
save_paths.append(save_path)
async with aiohttp.ClientSession() as session:
tasks = [download_file(session, url, save_path) for url, save_path in zip(urls, save_paths)]
await asyncio.gather(*tasks)
if __name__ == '__main__':
asyncio.run(main())