网站水军怎么做,免费微网站平台那个好,百度云 编辑wordpress,做网站赚多少Python httpx 模块详细讲解
一、引言
httpx 是一个用于发送 HTTP 请求的 Python 库#xff0c;它提供了简单易用的 API#xff0c;支持同步和异步请求#xff0c;并且具有出色的性能和灵活性。httpx 是 requests 的一个现代替代品#xff0c;它使用 httpcore 作为底层传输…Python httpx 模块详细讲解
一、引言
httpx 是一个用于发送 HTTP 请求的 Python 库它提供了简单易用的 API支持同步和异步请求并且具有出色的性能和灵活性。httpx 是 requests 的一个现代替代品它使用 httpcore 作为底层传输层支持 HTTP/1.1 和 HTTP/2 协议。
二、安装
你可以使用 pip 命令来安装 httpx
pip install httpx三、基本用法
发送 GET 请求
import httpxresponse httpx.get(https://www.example.com)
print(response.status_code) # 打印 HTTP 状态码
print(response.text) # 打印响应内容发送 POST 请求
import httpxpayload {key: value}
response httpx.post(https://www.example.com/post, jsonpayload)
print(response.status_code)
print(response.text)其他 HTTP 方法
httpx 同样支持 PUT、DELETE、HEAD 等其他 HTTP 方法。
response httpx.put(https://www.example.com/put, jsonpayload)
response httpx.delete(https://www.example.com/delete)
response httpx.head(https://www.example.com/head)四、响应处理
状态码
print(response.status_code) # 打印 HTTP 状态码响应头
print(response.headers) # 打印响应头信息响应内容
print(response.text) # 打印响应内容字符串形式
print(response.json()) # 如果响应内容是 JSON 格式可以使用此方法解析
print(response.content) # 打印响应内容字节形式错误处理
如果请求发生错误httpx 会抛出一个 httpx.HTTPError 异常。你可以使用 try-except 语句来捕获这个异常。
try:response httpx.get(https://www.example.com/invalid)print(response.text)
except httpx.HTTPError as err:print(err)五、高级特性
参数传递
在 GET 请求中你可以使用 params 参数来传递查询字符串。
payload {key1: value1, key2: value2}
response httpx.get(https://www.example.com/get, paramspayload)
print(response.url) # 打印完整的 URL包括查询字符串自定义请求头
你可以使用 headers 参数为请求添加自定义的头部信息。
headers {User-Agent: my-app/0.0.1}
response httpx.get(https://www.example.com, headersheaders)文件上传
使用 files 参数可以上传文件。
files {file: open(path/to/file, rb)}
response httpx.post(https://www.example.com/upload, filesfiles)认证
使用 auth 参数可以为请求添加 HTTP 认证。
from httpx import BasicAuthresponse httpx.get(https://www.example.com, authBasicAuth(username, password))超时设置
使用 timeout 参数可以为请求设置超时时间。
response httpx.get(https://www.example.com, timeout5.0) # 超时时间为 5 秒代理设置
使用 proxies 参数可以设置代理服务器。
proxies {http: http,https: http,
}response httpx.get(https://www.example.com, proxiesproxies)流式响应
对于大文件或长时间运行的响应你可能想要以流的方式处理数据而不是一次性加载整个响应体。httpx 支持流式响应允许你按需读取数据。
import httpxwith httpx.stream(GET, https://www.example.com/large-file) as response:for chunk in response.iter_content(chunk_size8192):# 处理每个数据块process(chunk)客户端会话
与 requests 的 Session 类似httpx 提供了 Client 和 AsyncClient 类用于创建客户端会话。这允许你在多个请求之间重用底层连接从而提高了性能。
import httpx# 创建同步客户端会话
with httpx.Client() as client:response1 client.get(https://www.example.com/api/data1)response2 client.get(https://www.example.com/api/data2)# 创建异步客户端会话
async with httpx.AsyncClient() as client:response1 await client.get(https://www.example.com/api/data1)response2 await client.get(https://www.example.com/api/data2)连接池管理
httpx 使用了连接池来管理底层 TCP 连接这有助于减少建立连接的开销。你可以通过配置 httpx.Client 或 httpx.AsyncClient 的连接池参数来定制连接池的行为。
# 同步客户端的连接池配置
with httpx.Client(limitshttpx.Limits(max_connections100, max_keepalive5)) as client:# ...# 异步客户端的连接池配置
async with httpx.AsyncClient(limitshttpx.AsyncLimits(max_connections100, max_keepalive5)) as client:# ...请求和响应模型
httpx 提供了 Request 和 Response 类这些类可以用来手动创建请求和响应对象。这在某些高级用法中可能很有用例如当你需要更细粒度的控制时。
import httpx# 创建请求对象
request httpx.Request(GET, https://www.example.com)# 发送请求并获取响应
with httpx.Client() as client:response client.send(request)# 处理响应
print(response.status_code)
print(response.text)错误处理
除了标准的 httpx.HTTPError 异常httpx 还提供了其他异常类用于处理不同的错误情况。
try:response httpx.get(https://www.example.com/invalid)response.raise_for_status()
except httpx.HTTPError as exc:print(fHTTP error occurred: {exc})
except httpx.RequestError as exc:print(fA request error occurred: {exc})
except httpx.ConnectError as exc:print(fConnection error occurred: {exc})工具和实用功能
httpx 还提供了一些实用的工具和函数如 httpx.URL 类用于解析和处理 URLhttpx.codes 模块包含 HTTP 状态码常量等。
from httpx import URL, codes# 解析 URL
parsed_url URL(https://www.example.com/path?queryvalue#fragment)
print(parsed_url.scheme) # 输出 https# 检查状态码是否表示成功
if response.status_code codes.ok:# 处理成功的响应pass
文章转载自: http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.qqnjr.cn.gov.cn.qqnjr.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.tytly.cn.gov.cn.tytly.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn http://www.morning.xnnpy.cn.gov.cn.xnnpy.cn http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.rhkgz.cn.gov.cn.rhkgz.cn http://www.morning.slwqt.cn.gov.cn.slwqt.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.tphrx.cn.gov.cn.tphrx.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.qbgdy.cn.gov.cn.qbgdy.cn http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn http://www.morning.fthqc.cn.gov.cn.fthqc.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.rfgc.cn.gov.cn.rfgc.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.gccdr.cn.gov.cn.gccdr.cn http://www.morning.fgxws.cn.gov.cn.fgxws.cn http://www.morning.kpwdt.cn.gov.cn.kpwdt.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.jcnmy.cn.gov.cn.jcnmy.cn http://www.morning.rdzlh.cn.gov.cn.rdzlh.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.gtylt.cn.gov.cn.gtylt.cn http://www.morning.rdsst.cn.gov.cn.rdsst.cn http://www.morning.qgghr.cn.gov.cn.qgghr.cn http://www.morning.cnhgc.cn.gov.cn.cnhgc.cn http://www.morning.bkppb.cn.gov.cn.bkppb.cn http://www.morning.xknsn.cn.gov.cn.xknsn.cn http://www.morning.mnslh.cn.gov.cn.mnslh.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.hfyll.cn.gov.cn.hfyll.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.lnbcg.cn.gov.cn.lnbcg.cn http://www.morning.nrzbq.cn.gov.cn.nrzbq.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.xqkjp.cn.gov.cn.xqkjp.cn http://www.morning.iznek.com.gov.cn.iznek.com http://www.morning.rjnm.cn.gov.cn.rjnm.cn http://www.morning.mhpmw.cn.gov.cn.mhpmw.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.tbstj.cn.gov.cn.tbstj.cn http://www.morning.rrgqq.cn.gov.cn.rrgqq.cn http://www.morning.rzsxb.cn.gov.cn.rzsxb.cn http://www.morning.mbfkt.cn.gov.cn.mbfkt.cn http://www.morning.hmjasw.com.gov.cn.hmjasw.com http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.nbpqx.cn.gov.cn.nbpqx.cn http://www.morning.swbhq.cn.gov.cn.swbhq.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.rmtmk.cn.gov.cn.rmtmk.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.wrlff.cn.gov.cn.wrlff.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.kjgrg.cn.gov.cn.kjgrg.cn http://www.morning.cpfbg.cn.gov.cn.cpfbg.cn http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.jynzb.cn.gov.cn.jynzb.cn http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.khntd.cn.gov.cn.khntd.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.phlrp.cn.gov.cn.phlrp.cn http://www.morning.tqygx.cn.gov.cn.tqygx.cn http://www.morning.pqhfx.cn.gov.cn.pqhfx.cn http://www.morning.ppgdp.cn.gov.cn.ppgdp.cn http://www.morning.btwrj.cn.gov.cn.btwrj.cn