aiohttp
简单使用
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
| import aiohttp import asyncio import async_timeout
async def request_dom(url,params=None): with async_timeout.timeout(10): async with aiohttp.ClientSession() as session: try: async with session.get(url,params=params) as resp: if resp.status == 200: return (await resp.text(encoding='utf-8')) else: print(f'------- error:{resp.status} -------') except aiohttp.ClientConnectionError: print('------- requests connection error -------')
async def main(): url = 'http://127.0.0.1:8000/' respone = await request_dom(url) return respone
if __name__ == '__main__': loop = asyncio.get_event_loop() html = loop.run_until_complete(main()) print(html)
|
返回内容
1
| return (await response.text())
|
返回二进制内容
1
| return (await response.read())
|
返回json:
1
| return (await response.json())
|
read(), json(), text()等方法会将所有的响应内容加载到内存。
流式响应内容
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
| import aiohttp import asyncio import async_timeout
async def request_dom(url,chunk_size=100,headers=None,cookies=None,params=None): with async_timeout.timeout(10): async with aiohttp.ClientSession(cookies=cookies) as session: try: async with session.get(url,headers=headers,params=params) as resp: if resp.status == 200: while True: chunk = await resp.content.read(chunk_size) if not chunk: break print(chunk) else: print(f'------- error:{resp.status} -------') except aiohttp.ClientConnectionError: print('------- requests connection error -------')
async def main(): url = 'http://127.0.0.1:8000/' await request_dom(url)
if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(main())
|