protobuf和flask结合高效数据传输

protobuf和flask结合

  • Protobuf(Google Protocol Buffers)是google开发的的一套用于数据存储,网络通信时用于协议编解码的工具库.它和XML和Json数据差不多,把数据已某种形式保存起来.Protobuf相对与XML和Json的不同之处,它是一种二进制的数据格式,具有更高的传输,打包和解包效率

flask使用protobuf

  • 首先安装protobuf

    pip install protobuf
    
  • 安装protoc, 下载地址protobuf,我用的是windows 64位,去github下载 protoc-3.13.0-win64.zip

  • 解压到项目下:

    protobuf和flask结合高效数据传输

  • 安装protobuf插件,settings->plugins->Marketplace,找到下面2个进行安装

    protobuf和flask结合高效数据传输

  • 编辑ProtobufRR.proto文件

    syntax = "proto3";
    
    
    // 响应体
    message Response {
      int32 returncode = 1;
      message data {
        string username = 1;
        int32 age = 2;
      }
      repeated data dataList = 2;
      string message = 3;
    }
    
    // 请求体
    message Request {
      string data = 1;
      int32 page = 2;
      int32 pageSize = 3;
    }
    
    
  • 将protobufRR.proto转换python

    bin\protoc.exe --python_out=./ ProtobufRR.proto
    
  • 此时会生成ProtobufRR_pd2.py文件

  • 编写flask服务端

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    """
    # Author Xu Junkai
    # coding=utf-8
    # @Time    : 2021/4/3 0:23
    # @Site    :
    # @File    : manage.py
    # @Software: PyCharm
    """
    from ProtobufRR_pb2 import Request, Response
    from flask import Flask, request
    app=Flask(__name__)
    @app.route("/my_protobuf", methods=["POST"])
    def my_protobuf():
        # 解析请求
        request_data = Request()
        request_data.ParseFromString(request.get_data())
        print("data",request_data.data)# data user
        print("page",request_data.page)# page 1
        print("pageSize",request_data.pageSize)# pageSize 10
        # 编写响应
        response = Response()
        response.returncode = 200
        response.message = "成功"
        d1 = response.data()
        d1.username = "小明"
        d1.age = 23
        d2 = response.data()
        d2.username = "小红"
        d2.age = 21
        response.dataList.append(d1)# 添加dataList里
        response.dataList.append(d2)
        return response.SerializeToString(), 200
    
    if __name__ == '__main__':
        app.run("127.0.0.1", port=5000)
    
    
  • 编写测试脚本

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    """
    # Author Xu Junkai
    # coding=utf-8
    # @Time    : 2021/4/3 0:34
    # @Site    :
    # @File    : request_server.py
    # @Software: PyCharm
    """
    from ProtobufRR_pb2 import Request, Response
    import requests
    def test_my_protobuf():
        """
        测试 protobuf
        :return:
        """
        request_data = Request()
        request_data.data = "user"
        request_data.page = 1
        request_data.pageSize = 10
        req_data = request_data.SerializeToString()# 序列化
        response = requests.post("http://127.0.0.1:5000/my_protobuf", data=req_data)
        res = Response()
        res.ParseFromString(response.content)# 反序列化
        print(res.returncode)# 200
        print(res.message)# 成功
        print(res.dataList)
        """
        [username: "\345\260\217\346\230\216"
        age: 23
        , username: "\345\260\217\347\272\242"
        age: 21
        ]
        """
        for d in res.dataList:
            print(f"username:{d.username}, age:{d.age}")
            # username:小明, age:23
            # username:小红, age:21
    if __name__ == '__main__':
        test_my_protobuf()
    
    
  • 参考:

    https://blog.csdn.net/u013210620/article/details/81317731

    https://blog.csdn.net/weixin_39841709/article/details/111292232

    https://github.com/protocolbuffers

上一篇:springboot2+netty+protobuf(精品)


下一篇:golang-protobuf配置和使用