3.除了上面的get方法外,會話還支持post,put,delete....等

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章