Httpx

作者: long | 2025-03-16

httpx是一个现代化的HTTP客户端库,支持同步和异步请求,功能比requests更强大。

安装:

pip install httpx

同步请求

import httpx

response = httpx.get('https://api.example.com/data')
print(response.status_code)
print(response.json())

异步请求

import httpx
import asyncio

async def fetch_data():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://api.example.com/data')
        print(response.json())

asyncio.run(fetch_data())