aiohttp使用详解
基本语法
async with aiohttp.request('GET','https://github.com') as r: await r.text()
指定编码 await resp.text(encoding=‘windows-1251’) 适合读取图像等 await resp.read() request案例 超时处理timeout async with session.get('https://github.com', timeout=60) as r:
#使用示例, 进行一次请求 import aiohttp, asyncio async def main():#aiohttp必须放在异步函数中使用 async with aiohttp.request('GET', 'https://api.github.com/events') as resp: json = await resp.json() print(json) loop = asyncio.get_event_loop() loop.run_until_complete(main())
------------------------------------------------------------------------------
#使用示例,进行多次请求
import aiohttp, asyncio async def main():#aiohttp必须放在异步函数中使用 tasks = [] [tasks.append(fetch('https://api.github.com/events?a={}'.format(i))) for i in range(10)]#十次请求 await asyncio.wait(tasks) async def fetch(url): async with aiohttp.request('GET', url) as resp: json = await resp.json() print(json) loop = asyncio.get_event_loop() loop.run_until_complete(main()) ------------------------------------------------------------------------------
#使用示例,进行多次请求,并限制同时请求的数量
import aiohttp, asyncio async def main(pool):#aiohttp必须放在异步函数中使用 tasks = [] sem = asyncio.Semaphore(pool)#限制同时请求的数量 [tasks.append(control_sem(sem, 'https://api.github.com/events?a={}'.format(i))) for i in range(10)]#十次请求 await asyncio.wait(tasks) async def control_sem(sem, url):#限制信号量 async with sem: await fetch(url) async def fetch(url): async with aiohttp.request('GET', url) as resp: json = await resp.json() print(json) loop = asyncio.get_event_loop() loop.run_until_complete(main(pool=2))
上面的示例中可以正确的使用协程进行请求,但是由于aiohttp自身的原因会报 Unclosed client session 的警告。官方不推荐使用aiohttp.request的方式请求,可以将 aiohttp.request 换成
求,可以将 aiohttp.request 换成
aiohttp.ClientSession(**kw).request的方式即可 session import aiohttp async with aiohttp.ClientSession() as session: async with session.get('https://api.github.com/events') as resp: print(resp.status) print(await resp.text())
我们创建了一个 ClientSession 对象命名为session,然后通过session的get方法得到一个 ClientResponse 对象,命名为resp,get方法中传入了一个必须的参数url,就是要获得源码的http url。至此便通过协程完成了一个异步IO的get请求。
带参数
session.post('http://httpbin.org/post', data=b'data') session.put('http://httpbin.org/put', data=b'data') session.delete('http://httpbin.org/delete') session.head('http://httpbin.org/get') session.options('http://httpbin.org/get') session.patch('http://httpbin.org/patch', data=b'data')
不要为每次的连接都创建一次session,一般情况下只需要创建一个session,然后使用这个session执行所有的请求。
每个session对象,内部包含了一个连接池,并且将会保持连接和连接复用(默认开启)可以加快整体的性能。
#使用示例
import aiohttp, asyncio async def main(pool):#启动 sem = asyncio.Semaphore(pool) async with aiohttp.ClientSession() as session:#给所有的请求,创建同一个session tasks = [] [tasks.append(control_sem(sem, 'https://api.github.com/events?a={}'.format(i), session)) for i in range(10)]#十次请求 await asyncio.wait(tasks) async def control_sem(sem, url, session):#限制信号量 async with sem: await fetch(url, session) async def fetch(url, session):#开启异步请求 async with session.get(url) as resp: json = await resp.json() print(json) loop = asyncio.get_event_loop() loop.run_until_complete(main(pool=2))
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律