Skip to navigation
Download asynchronously images with aiohttp
30.11.17
#!/usr/bin/env python import sys import asyncio import aiohttp class Pictures(): def __init__(self): self.sem = asyncio.Semaphore(50) async def _get(self,session,url): async with self.sem: async with session.get(url) as r: img = await r.content.read() return {'data':img,'name':r.url.raw_name} async def test(self,session,url): async with self.sem: async with session.get('http://127.0.0.1:8080/{}'.format('x')) as r: print(r.url) async def get(self,urls): files = [] async with aiohttp.ClientSession() as session: tasks = [] for i in urls: #tasks.append(self.test(session,i)) tasks.append(self._get(session,i)) r = list(await asyncio.wait(tasks)) for i in r: for ii in i: files.append(ii.result()) return files
http://aiohttp.readthedocs.io/en/stable/
Reply
Anonymous
Information Epoch 1740344177
Make every program a filter.
Home
Notebook
Contact us