-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_client.py
More file actions
36 lines (28 loc) · 806 Bytes
/
http_client.py
File metadata and controls
36 lines (28 loc) · 806 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import aiohttp
import asyncio
async def getPage(session, url):
response = {
'success': False,
'url': url,
'resText': ''
}
try:
async with session.get(url) as res:
response['success'] = True
response['resText'] = await res.text()
return response
except Exception:
return response
async def getAllPages(session, urls):
tasks = []
for url in urls:
task = asyncio.create_task(getPage(session, url))
tasks.append(task)
responses = await asyncio.gather(*tasks)
return responses
async def init(urls):
async with aiohttp.ClientSession() as session:
data = await getAllPages(session, urls)
return data
def makeParallelReq(urls):
return asyncio.run(init(urls))