pip install aiohttp
客户端
快速开始
import aiohttpimport asyncioasync def main(): # 创建客户端会话 async with aiohttp.ClientSession() as session: # 发起get请求 async with session.get('https://baidu.com') as response: # 返回状态码 print("Status", response.status) # 返回内容格式 print("Content-type",response.headers["content-type"]) # 对text格式进行解析 html = await response.text() print(html[:16]) # 获取JSON数据 async with session.get(url) as response: print(response.json()) # 发起带有JSON数据的post请求 async with session.post(url,json={"key":"value"}) as response: # 发送form-encoded格式数据 {"form":{"key":"value"}} async with session.post(url,data={"key":"value"}) as response: # 发送multipart-encoded格式数据 files = {"file",open("data.xls","rb"),filename="report.xls"} await session.post(url,data=files)# 创建异步事件循环。loop = asyncio.get_event_loop()loop.run_until_complete(main())