美文网首页
FastAPI实现Protobuf服务端

FastAPI实现Protobuf服务端

作者: 还是那个没头脑 | 来源:发表于2021-11-04 12:22 被阅读0次

1. 环境配置

1.1 安装python3
1.2 使用pip安装FastAPI和Protocbuf
pip3 install fastapi
pip3 install uvicorn
pip3 install protobuf
1.3 配置protoc

github下载对应和你PC对应版本的protoc。

1.4 pycharm安装protobuf插件

在plugins->Marketplace 搜索并安装 protobuf support 插件

2. .proto配置文件编写

syntax = "proto3";

message Response {
  int32 code = 1;

  message data {
    string id = 1;
    string title = 2;
  }

  repeated data dataList = 2;
}

message Request {
  string channelId = 1;  //频道id
  int32 page = 2;         //页码
  int32 pageSize = 3;     //页条目数
}

3. 使用protoc把配置文件转成python代码

protoc --python_out=. ./test.proto

注:protoc配置到环境变量

然后当前目录下会生成与.proto文件名相似的python文件。


4 编写FastAPI服务端

import uvicorn
from fastapi import FastAPI, Form
from fastapi import Response as fres
from test_pb2 import Request, Response

app = FastAPI()

@app.post('/protobuf')
async def _protobuf(pyload: bytes = Form(...)):
    # 解析请求
    req = Request()
    req.ParseFromString(pyload)
    print(req)

    # 编写响应
    res = Response()
    res.code = 200
    d1 = res.data()
    d1.id = "1"
    d1.title = "小明"
    d2 = res.data()
    d2.id = "2"
    d2.title = "李雷"
    res.dataList.append(d1)
    res.dataList.append(d2)
    print(res.SerializeToString())

    return fres(res.SerializeToString())

if __name__ == '__main__':
    uvicorn.run(
        app='main:app',
        host="0.0.0.0",
        port=8899,
        workers=4,
        reload=True,
        debug=True)

需重点记住这两个api。SerializeToStringParseFromString,分别对应的是序列化和反序列化。

5. 编写python脚本测试协议

import requests
from test_pb2 import Request, Response

def test_protobuf():
    """
    test
    :return:
    """
    req = Request()
    req.channelId = "people"
    req.page = 1
    req.pageSize = 10
    req_bytes = req.SerializeToString()
    data = {'pyload': req_bytes}
    response = requests.post("http://127.0.0.1:8899/protobuf", data=data)

    res = Response()
    res.ParseFromString(response.content)
    print(res)
    for i in res.dataList:
        print(i.title)


if __name__ == '__main__':
    test_protobuf()

谢鸣

感谢 @风中的承诺 大佬的 Protobuf后台python实现 一文,将大佬的Flask改用FastAPI实现

相关文章

  • FastAPI实现Protobuf服务端

    1. 环境配置 1.1 安装python3[https://www.python.org/downloads/]...

  • Netty与Websocket使用protobuf实现聊天系统

    服务端:Netty序列化数据协议:protobuf前端通讯:WebSocket 前端: 服务端:

  • WCF服务使用ProtoBuf传输

    ProtoBuf的优缺点不再叙说,本篇主要介绍是WCF服务端和WCF客户端通过ProtoBuf传输数据的例子。 1...

  • FastAPI CBV的实现

    FastAPI天生不支持CBV 在Starlette层面上,还提供有CBV的支持,但是在FastAPI的实现,都是...

  • protobuf安装和嵌套定义

    背景 博主因为公司项目原因,客户端和服务端通信采用了protobuf协议,关于protobuf协议,不明白的自行百...

  • Go protobuf

    使用protobuf实现节点间通信、编码报文以提高传输效率 protobuf全程Protocol Buffers,...

  • maven插件编译proto文件(Java篇)

    Idea+protobuf-maven-plugin插件编译proto文件,自动生成客户端和服务端代码 首先,定义...

  • grpc初探

    1 grpc的定义 grpc good rpc grpc使用protobuf文件声明服务,服务端和客户端都通使用...

  • FastAPI WebSocket CBV实现

    FastAPI的WebSocket也未提供CBV方案。造个轮子WebSocketCBV是对starlette的We...

  • FastAPI 完整CBV实现

    10.23更新:装饰器做了增强 就FastAPI的CBV实现,之前出了篇文章。做了个简单的实现方法。今天逛gith...

网友评论

      本文标题:FastAPI实现Protobuf服务端

      本文链接:https://www.haomeiwen.com/subject/xbtkzltx.html