欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中的httpx異步請求

 更新時間:2024年06月29日 14:32:36   作者:像風一樣的男人@  
這篇文章主要介紹了python中的httpx異步請求方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

異步支持

HTTPX默認情況下提供標準的同步API,但是如果需要,還可以為你提供異步客戶端的選項 。

要發(fā)出異步請求,你需要一個httpx.AsyncClient

import asyncio
import httpx

async def main():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://example.org/')

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(main())
finally:
    loop.close()

發(fā)出請求

AsyncClient.get(url, ...)
AsyncClient.options(url, ...)
AsyncClient.head(url, ...)
AsyncClient.post(url, ...)
AsyncClient.put(url, ...)
AsyncClient.patch(url, ...)
AsyncClient.delete(url, ...)
AsyncClient.request(url, ...)
AsyncClient.send(url, ...)

流式響應

Response.aread()
Response.aiter_bytes()
Response.aiter_text()
Response.aiter_lines()
Response.aiter_raw()

實例

import asyncio
import httpx

async def re():
    async with httpx.AsyncClient() as client:
        res = await client.get('https://www.baidu.com')
        print(res.text)
        return res.text

loop = asyncio.get_event_loop()
task = [re(), ] # 把任務放入數(shù)組,準備給事件循環(huán)器調(diào)用
loop.run_until_complete(asyncio.wait(task))
loop.close()

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論