testaio.py 440 B

12345678910111213141516
  1. import aiohttp
  2. import asyncio
  3. import async_timeout
  4. async def fetch(session, url):
  5. with async_timeout.timeout(10):
  6. async with session.get(url) as response:
  7. return await response.text()
  8. async def main(loop):
  9. async with aiohttp.ClientSession(loop=loop) as session:
  10. html = await fetch(session, 'http://www.baidu.com')
  11. print(html)
  12. loop = asyncio.get_event_loop()
  13. loop.run_until_complete(main(loop))