3.除了上面的get方法外,会话还支持post,put,delete....等
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')
node2:/root/python/20200525#cat t400.py
import asyncio,aiohttp
# -*- coding: utf-8 -*-
async def fetch_async(url,pdata):
print(url)
async with aiohttp.ClientSession() as session:#协程嵌套,只需要处理最外层协程即可fetch_async
async with session.post(url,data=pdata) as resp:
print(resp.url)
print(resp.status)
print(await resp.text())#因为这里使用到了await关键字,实现异步,所有他上面的函数体需要声明为异步async
data1={'aa': '111', 'bb': '111','cc':'111'}
data2={'aa': '222', 'bb': '222','cc':'222'}
data3={'aa': '333', 'bb': '333','cc':'333'}
tasks = [fetch_async('http://192.168.137.3:9000/test111/',data1), fetch_async('http://192.168.137.3:9000/test222/',data2),fetch_async('http://192.168.137.3:9000/test333/',data3)]
event_loop = asyncio.get_event_loop()
results = event_loop.run_until_complete(asyncio.gather(*tasks))
event_loop.close()
node2:/root/python/20200525#time python3 t400.py
http://192.168.137.3:9000/test111/
http://192.168.137.3:9000/test222/
http://192.168.137.3:9000/test333/
http://192.168.137.3:9000/test111/
200
aa=111&bb=111&cc=111
http://192.168.137.3:9000/test222/
200
aa=222&bb=222&cc=222
http://192.168.137.3:9000/test333/
200
aa=333&bb=333&cc=333
real 0m7.548s
user 0m0.462s
sys 0m0.055s