-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_II.py
executable file
·30 lines (24 loc) · 967 Bytes
/
sample_II.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
#!/usr/bin/env python3 # MUST be Python 3.5 or later
import aiohttp
import asyncio
async def fetch(session, url):
with aiohttp.Timeout(10):
async with session.get(url) as response:
return await response.text()
if __name__ == '__zmain__':
loop = asyncio.get_event_loop()
with aiohttp.ClientSession(loop=loop) as session:
html = loop.run_until_complete(
fetch(session, 'http://python.org'))
print(html)
URLS = '''http://python.org
http://golang.org
http://perl.org
http://ruby-lang.org'''.split()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
with aiohttp.ClientSession(loop=loop) as session:
coroutines = [fetch(session, url) for url in URLS]
pages = [loop.run_until_complete(coroutine) for coroutine in coroutines]
for url, page in zip(URLS, pages):
print('{}:\n{}=\n{:.100}\n'.format(url, '=' * len(url), page))