更新
This commit is contained in:
29
Python-100-Days/Day66-75/code/asyncio02.py
Normal file
29
Python-100-Days/Day66-75/code/asyncio02.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
|
||||
async def download(url):
|
||||
print('Fetch:', url)
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(url) as resp:
|
||||
print(url, '--->', resp.status)
|
||||
print(url, '--->', resp.cookies)
|
||||
print('\n\n', await resp.text())
|
||||
|
||||
|
||||
def main():
|
||||
loop = asyncio.get_event_loop()
|
||||
urls = [
|
||||
'https://www.baidu.com',
|
||||
'http://www.sohu.com/',
|
||||
'http://www.sina.com.cn/',
|
||||
'https://www.taobao.com/',
|
||||
'https://www.jd.com/'
|
||||
]
|
||||
tasks = [download(url) for url in urls]
|
||||
loop.run_until_complete(asyncio.wait(tasks))
|
||||
loop.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user