Python22_httpx网络请求httpx.AsyncClient这是 Python 中处理异步 HTTP 请求的核心类。httpx.AsyncClient 概览httpx.AsyncClient是HTTPX库的异步客户端基于asyncio构建支持非阻塞的 HTTP/1.1 和 HTTP/2 请求性能远超同步的requests库。基本使用1. 安装pipinstallhttpx# 如需 HTTP/2 支持pipinstallhttpx[http2]2. 简单请求importhttpximportasyncioasyncdefmain():# 创建客户端推荐用 async with 管理生命周期asyncwithhttpx.AsyncClient()asclient:# GET 请求responseawaitclient.get(https://api.github.com)print(response.status_code)# 200print(response.json())# 解析 JSONasyncio.run(main())核心特性详解3. 请求方法asyncwithhttpx.AsyncClient()asclient:# 各类 HTTP 方法r1awaitclient.get(url,params{key:value})r2awaitclient.post(url,json{data:value})# JSON 体r3awaitclient.post(url,data{form:field})# 表单数据r4awaitclient.put(url,contentbraw bytes)# 原始字节r5awaitclient.patch(url,files{file:open(a.jpg,rb)})r6awaitclient.delete(url)r7awaitclient.head(url)r8awaitclient.options(url)4. 高级配置clienthttpx.AsyncClient(# 超时设置连接、读取、写入timeouthttpx.Timeout(10.0,connect5.0),# 请求头headers{User-Agent:MyApp/1.0},# 基础 URL后续请求可写相对路径base_urlhttps://api.example.com/v1,# 启用 HTTP/2http2True,# 跟随重定向follow_redirectsTrue,# 验证 SSL开发时可关闭verifyTrue,# 或 verifyFalse不推荐生产环境# 代理proxieshttp://localhost:8080,# 或按协议区分proxies{http://: ..., https://: ...}# 认证auth(username,password),# Basic Auth# 或 authhttpx.BearerToken(token)# Cookie 持久化cookies{session:abc123},# 限制连接池limitshttpx.Limits(max_connections100,max_keepalive_connections20))# 使用 base_url 后responseawaitclient.get(/users)# 实际请求 https://api.example.com/v1/users5. 并发请求核心优势importasyncioimporthttpxasyncdeffetch(client,url):responseawaitclient.get(url)returnresponse.json()asyncdefmain():urls[https://api.github.com/users/octocat,https://api.github.com/users/torvalds,https://api.github.com/users/gvanrossum]asyncwithhttpx.AsyncClient()asclient:# 并发执行所有请求tasks[fetch(client,url)forurlinurls]resultsawaitasyncio.gather(*tasks)print(results)asyncio.run(main())6. 流式响应大文件下载asyncwithhttpx.AsyncClient()asclient:# 流式读取避免内存溢出asyncwithclient.stream(GET,https://example.com/large-file.zip)asresponse:asyncforchunkinresponse.aiter_bytes():# 处理每个数据块print(fReceived{len(chunk)}bytes)# 或流式文本asyncwithclient.stream(GET,url)asresponse:asyncforlineinresponse.aiter_lines():print(line)7. 请求/响应钩子deflog_request(request):print(f→ Request:{request.method}{request.url})deflog_response(response):print(f← Response:{response.status_code})asyncwithhttpx.AsyncClient(event_hooks{request:[log_request],response:[log_response]})asclient:awaitclient.get(https://httpbin.org/get)与同步 Client 对比特性httpx.Clienthttpx.AsyncClient执行方式同步阻塞异步非阻塞适用场景脚本、简单任务高并发、Web 服务性能一般极高可处理数千并发使用方式with client:async with client:依赖无额外依赖需要asyncio最佳实践importhttpximportasyncioclassAPIClient:封装示例带重试和错误处理的异步客户端def__init__(self):self.clienthttpx.AsyncClient(base_urlhttps://api.example.com,timeout30.0,http2True,limitshttpx.Limits(max_connections50))asyncdef__aenter__(self):returnselfasyncdef__aexit__(self,*args):awaitself.client.aclose()# 确保关闭asyncdefget_user(self,user_id:int):try:responseawaitself.client.get(f/users/{user_id})response.raise_for_status()# 自动抛出 4xx/5xx 异常returnresponse.json()excepthttpx.HTTPStatusErrorase:print(fHTTP error:{e.response.status_code})raiseexcepthttpx.RequestErrorase:print(fRequest failed:{e})raise# 使用asyncdefmain():asyncwithAPIClient()asapi:userawaitapi.get_user(123)print(user)asyncio.run(main())常见异常处理异常说明httpx.RequestError网络连接错误httpx.HTTPStatusErrorHTTP 错误状态码需调用raise_for_status()httpx.TimeoutException请求超时httpx.ConnectError连接失败httpx.AsyncClient是现代 Python 异步编程中处理 HTTP 请求的首选工具特别适合FastAPI/Starlette等异步 Web 框架的后端服务调用。