asyncio02.py 696 B

1234567891011121314151617181920212223242526272829
  1. import asyncio
  2. import aiohttp
  3. async def download(url):
  4. print('Fetch:', url)
  5. async with aiohttp.ClientSession() as session:
  6. async with session.get(url) as resp:
  7. print(url, '--->', resp.status)
  8. print(url, '--->', resp.cookies)
  9. print('\n\n', await resp.text())
  10. def main():
  11. loop = asyncio.get_event_loop()
  12. urls = [
  13. 'https://www.baidu.com',
  14. 'http://www.sohu.com/',
  15. 'http://www.sina.com.cn/',
  16. 'https://www.taobao.com/',
  17. 'https://www.jd.com/'
  18. ]
  19. tasks = [download(url) for url in urls]
  20. loop.run_until_complete(asyncio.wait(tasks))
  21. loop.close()
  22. if __name__ == '__main__':
  23. main()