分享

Python的下一代http客户端

 流形sbz 2023-10-12 发布于甘肃

简介

之前我们介绍过使用 requests 来进行 http 操作,本篇介绍另一个功能非常类似的第三方库 httpx,它提供了同步和异步的 API,同时支持 HTTP/1.1HTTP/2,是一个全功能的 HTTP 客户端。

安装

使用 pip 安装,执行命令

pip install httpx

在安装 python 库的同时,还安装了命令行工具 httpx.exe

Python的下一代http客户端

来看几个简单的示例

# get方法请求urlhttpx.exe https://github.com -m get# post方法请求url,同时上传一个文本文件httpx.exe https:// -m post -f test.txt

基本使用

还是拿之前的 flask 后端例子来讲

from flask import Flask, jsonify, requestfrom flask_restful import Api, Resource, reqparseUSERS = [ {'name': 'zhangsan'}, {'name': 'lisi'}, {'name': 'wangwu'}, {'name': 'zhaoliu'}]class Users(Resource): def get(self): return jsonify(USERS) def post(self): args = reqparse.RequestParser() \ .add_argument('name', type=str, location='json', required=True, help='名字不能为空') \ .parse_args() if args['name'] not in USERS: USERS.append({'name': args['name']}) return jsonify(USERS) def delete(self): USERS = [] return jsonify(USERS)class UserId(Resource): def __init__(self): self.parser = reqparse.RequestParser() self.parser.add_argument('name', type=str) self.parser.add_argument('age', type=int) def get(self, userid): datas = self.parser.parse_args() return jsonify( {'name': USERS[int(userid)].get('name'), 'age': datas.get('age')} ) def post(self, userid): file = request.files['file'] file.save('flask_file.txt') return jsonify({ 'msg' : 'success' })app = Flask(__name__)api = Api(app, default_mediatype='application/json')api.add_resource(Users, '/users')api.add_resource(UserId, '/user/<userid>')app.run(host='0.0.0.0', port=5000, use_reloader=True, debug=True)

启动后端服务后,接着来看看客户端的请求。httpx 的基本用法和 requests 近乎相同,很多时候,只需要将原来的代码中的 requests 换成 httpx 就行。

import httpx# 使用get方法r = httpx.get('http://127.0.0.1:5000/users')# http返回码print(r.status_code)# http头print(r.headers['content-type'])# 也可以使用 r.headers.get('content-type')# 接口返回的jsonprint(r.json())
Python的下一代http客户端
import httpximport jsonparam = {'name': 'xugaoxiang'}headers = {'Content-type': 'application/json'}# post请求r = httpx.post('http://127.0.0.1:5000/users', data=json.dumps(param), headers=headers)print(r.status_code)print(r.json())
Python的下一代http客户端
import httpx# delete请求r = httpx.delete('http://127.0.0.1:5000/users')print(r.json())print(r.status_code)
Python的下一代http客户端

除此之外,像 putheadoptions 方法的请求也是类似的,这里就不再举例了

r = httpx.put(url, data={'key': 'value'})r = httpx.head(url)r = httpx.options(url)

高级用法

上面示例中的用法是 httpx 提供的 top-level API,这在写一些测试脚本或者做系统原型时问题不大,但真正要在实际项目中去用的话,就会有性能上的问题。这是因为 httpx 在进行每一次的请求时都会去重新建立一个链接,也就是原有的链接没有被复用,这在高并发的情况就显得特别低效。

类似于 requests 模块中的 Sessionhttpx 提供了 Client,它会使用 http 连接池,大大减少链接重新建立的次数,减少 cpu 的使用率,降低了网络拥堵,提升系统效率。

Client 的使用比较简单,推荐的做法是将 Client 作为 context 管理器,看下面的示例

import httpxwith httpx.Client() as client:    # 请求部分,将原来的 httpx 换成 client 就可以了,参数是一样的    r = client.get('http://127.0.0.1:5000/users')    print(r.json())    print(r.status_code)

同步请求与异步请求

默认情况下,httpx 提供的是标准同步 API,如果想使用异步请求,可以这样

import httpximport asyncioasync def make_async_request(): async with httpx.AsyncClient() as client: r = await client.get('http://127.0.0.1:5000/users') print(r.json()) print(r.status_code)asyncio.run(make_async_request())
Python的下一代http客户端

http2支持

要使能 http2,我们需要额外安装库 httpx[http2]

# 这个包名取的太奇怪了pip install httpx[http2]

然后在初始化 client 的时候加上 http2 的支持

with httpx.Client(http2=True) as client: r = client.get('http://127.0.0.1:5000/users')

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多