-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
47 lines (32 loc) · 1.2 KB
/
main.py
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
37
38
39
40
41
42
43
44
45
46
47
import asyncio
import aiohttp
urls = ['http://google.com', 'http://localhost:5000/', 'http://localhost:5000/']
# url = 'http://localhost:5000/'
async def call_url(url):
print('Starting {}'.format(url))
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.text()
status = response.status
# statusCode = response.statusCode
print('url : {}, data length: {}, status: {}'.format(url, len(data), status))
return data
futures = [call_url(url) for url in urls]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(futures))
# http://localhost:5000/: 29
# http://localhost:5000/: 29
# http://google.com: 10815
# --------------------------------------------
# async def fetch(session, url):
# print('Fetching')
# async with session.get(url) as response:
# return await response.text()
# async def main():
# async with aiohttp.ClientSession() as session:
# for url in urls:
# print(url)
# html = await fetch(session, url)
# print(html)
# loop = asyncio.get_event_loop()
# loop.run_until_complete(main())