python中的httpx异步请求

HTTPX 是一个 Python 3.6+ 用于异步和同步 HTTP 请求的库。它提供了一个简单、易用的接口,并兼容 requests API。

以下是一个使用 HTTPX 发送异步 GET 请求的示例:

bash 复制代码
import asyncio

import httpx



async def fetch_async(url):

    async with httpx.AsyncClient() as client:

        response = await client.get(url)

        return response.text



async def main():

    url = 'http://httpbin.org/get'

    content = await fetch_async(url)

    print(content)



# 运行事件循环

asyncio.run(main())

在这个例子中,我们定义了一个异步函数 fetch_async,它使用了 HTTPX 的 AsyncClient 来发送异步 GET 请求。然后我们定义了一个 main 函数,它运行异步函数并打印结果。最后,我们使用 asyncio.run 来运行事件循环并启动异步任务。

相关推荐
从零开始学习人工智能8 小时前
【踩坑实录】WSL2 解决 onnxruntime\-gpu ImportError: libcudart\.so\.13 无 CUDA13 运行库问题
python
卷无止境10 小时前
在 awesome-fastapi 里,哪些库值得一看?
后端·python
zhanghaha131410 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
Python私教10 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
Python私教11 小时前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi
卷无止境11 小时前
FastAPI 的Admin面板生态
后端·python
ctlover11 小时前
Streamlit 框架
python
ι:12 小时前
MATLAB 与 Python 搭建无人机地面站:优势、劣势与选型逻辑
python·matlab·无人机
船厂电气自动化ai大模型12 小时前
AI大模型与数学 第32课 函数凹凸性与二阶导数:拐点求解、凹凸区间计算(10道二阶导数计算题)
数据结构·人工智能·python·深度学习·算法
jufeng130712 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 7 篇】
python·ai agent·权限系统