概念

Google开源的一个序列化框架,类似于xml , json。最大的特点是基于 二进制,比传统的xml表示同样的内容要短小很多。亦可定义一些可选字段,用于服务端与客户端通信

优点:

1:序列化后体积相比Json和XML很小,适合网络传输

2:支持跨平台多语言

3:消息格式升级和兼容性还不错

4:序列化反序列化速度很快,快于Json的处理速度

缺点:

1、以二进制的方式存储,除非你有 .proto 定义,否则你没法直接读出 Protobuf 的任何内容。

2、功能简单,无法用来表示复杂的概念。

标准数据类型

一个标量消息字段可以含有一个如下的类型——该表格展示了定义于.proto文件中的类型,以及与之对应的、在自动生成的访问类中定义的类型

基于序号的协议字段映射(类似key-value结构)

新建 test.proto

在消息中承载的数据分别对应于每一个字段都有一个名字和一种类型。

syntax = "proto3";package  WeightEstimationUpdate;
option   java_package = "com.muyuan.platform.bar.patrol.pro";
// 请求包基类(没有附加数据,通信包不重新定义直接使用基类包)
message BaseRequestCommon
{string      DeviceId = 1;    // 设备编号string      MsgID = 2;    // 消息ID,用UUIDstring      Timestamp = 3;    // unix时间戳(秒)uint32      Cmd = 4;    // 指令信息bytes       payLoad = 5;  // 消息体
}// 上报
message DeviceRegist
{string  version = 1;    // string  macAddr = 2;    //
}// 下发
message PushUpgradeInfo
{string  version = 1;            // 版本号string  packageName = 2;          // string  packageMd5 = 3;          // string  packageUrl = 4;          //
}// 上报
message ReportWeightEstimationStatus
{string      version = 1;    // string      state = 2;      //
}// 指令列表
enum EmCmd
{CMD_NONE = 0x0000;       // 指令开始范围//-----------------服务器端主动下发到设备端信令定义开始------------------CMD_S2C_PUSH_UPGRADE_INFO = 0x0013;    // 下发(协议包:PushUpgradeInfo)//-----------------服务器端主动下发到设备端信令定义结束-----------------//-----------------设备端主动上报到服务端信令定义开始-------------------CMD_C2S_REPORT_REGIST = 0x0060;   // 注册(协议包:WeightEstimationRegist)CMD_C2S_REPORT_FAULT = 0x0061;   // 上报故障(协议包:ReportFault)CMD_C2S_REPORT_WEIGHT_ESTIMATION_STATUS = 0x0063;    // 上报状态信息(协议包:WeightEstimationStatus)//-----------------设备端主动上报到服务端信令定义结束-----------------CMD_END = 0xFFFF;        // 指令结束范围
}

情况1: 收到通信信息

import test_pb2 as weight_pd
base_request_common_obj = weight_pd.BaseRequestCommon()
base_request_common_obj.ParseFromString(msg)
payload = base_request_common_obj.payLoad
push_upgrade_info_obj = weight_pd.PushUpgradeInfo()
push_upgrade_info_obj.ParseFromString(payload)
update_version = push_upgrade_info_obj.version
update_zip_filename = push_upgrade_info_obj.packageName
# 反向解析即可

情况2:发送通信信息

import test_pb2 as weight_pd
base_request_common = weight_pd.BaseRequestCommon()
base_request_common.DeviceId = deviceId
base_request_common.MsgID = str(uuid.uuid4())
base_request_common.Timestamp = str(int(time.time()))
# change
item_list = weight_pd.EmCmd.items()
#此为 protobuf 3.0.0 版本的
weight_dict = listtuple_dict(item_list)
base_request_common.Cmd = weight_dict.get("CMD_C2S_REPORT_WEIGHT_ESTIMATION_STATUS")
#此为 protobuf 最新版本
# base_request_common.Cmd = weight_pd.EmCmd.CMD_C2S_REPORT_WEIGHT_ESTIMATION_STATUS
report_weight_estimation_status = weight_pd.ReportWeightEstimationStatus()
report_weight_estimation_status.version = self.version
report_weight_estimation_status.state = state
base_request_common.payLoad = report_weight_estimation_status.SerializeToString()
serializeToString = base_request_common.SerializeToString()
#  serializeToString 即为 二进制数据流
def listtuple_dict(item_list):weight_cmd_dict = {}for k, v in item_list:weight_cmd_dict.setdefault(k, v)return weight_cmd_dict```
编译为python
protoc -I=. --python_out=. ./xxxxx.proto

python使用protobuf相关推荐

  1. Python对Protobuf进行序列化与反序列化

    Python Protobuf 1.了解Protobuf: 1.1 Protobuf语法介绍: 2. Python使用Protobuf:(windows平台上) 1.了解Protobuf: 我们在使用 ...

  2. python读写protobuf

    0.     前期准备 官方protobuf定义 https://code.google.com/p/protobuf/ python使用指南 https://developers.google.co ...

  3. 【Unity】安装配置Python使用protobuf转换Excel表格数据并在unit中使用

    前言: 之前使用NPOI插件编写的导表工具,其实就是直接将数据进行序列化,解析时还需要进行反序列化,步骤比较繁复,最近看到Google的一个开源的项目protobuf,不仅可以用于进行excel表格数 ...

  4. python操作protobuf基础

    概要 Protobuf是一种数据格式,它支持跨语言,它有以下好处: 它是按照二进制方式进行存储,同时数据协议提前定义好(这样不需要存储key),默认值字段不进行传输,因此压缩效率很高: 即使数据格式发 ...

  5. 【库安装】windows下Python安装protobuf

    Windows下安装protobuf https://github.com/protocolbuffers/protobuf/releases 1.到上边的网址能找到一个包,下载下面图中的两个包 2. ...

  6. Python使用protobuf序列化和反序列化

    protobuf介绍 protobuf是一种二进制的序列化格式,相对于json来说体积更小,传输更快. 安装protobuf 安装protobuf的目的主要用来将proto文件编译成python.c. ...

  7. python安装protobuf教程

    python和protoc(编译proto到各个语言) 首先下载protobuf源代码(支持各种语言实现):https://github.com/google/protobuf #如果在执行编译命令时 ...

  8. c python通信protobuf_python 处理protobuf协议

    背景:需要用django基于python3模拟一个http接口,请求是post方式,body是protobuf string,返回也是protobuf string 设计:django获取pb str ...

  9. protobuf序列化协议python教程

    全栈工程师开发手册 (作者:栾鹏) 架构系列文章 Protobuf? 是什么? Google Protocol Buffer(简称 Protobuf)是一种轻便高效的结构化数据存储格式,平台无关.语言 ...

最新文章

  1. 4.4、Bootstrap V4自学之路------组件---表单
  2. SQL Server 取日期时间部分
  3. twitter api_Java应用程序上的Twitter API
  4. 高并发网络架构解决方案分析
  5. 用C#做短信CMPP2.0/3.0协议 支持扩展号支持物理网卡
  6. C#OOP之一面向对象简介
  7. 软件汉化工具:eXeScope
  8. 分享一个Ubuntu16.0.4安装MySQL5.7脚本
  9. Matplotlib——基本用法
  10. 机器学习笔记0_学习资料整理
  11. 一个简单的教学管理系统(SQL实现)
  12. SQL——数据各项操作代码实现
  13. dxf解析python_Python 读取DXF文件
  14. Python爬虫—手机销量
  15. PID串口助手的第一部分:串口通信
  16. 天数怎么换算成月_excel表中,怎么把日期数转换成月份数呢?
  17. html下拉式日历,C#实现日历样式的下拉式计算器
  18. 海康威视人脸、指纹一体机SDK封装 for PowerBuilder 说明
  19. 【Spring】面向切面编程详解(AOP)
  20. 离心泵CAE_2_ICEM剖分网格_1_进口延伸段

热门文章

  1. 银豹 PYTHON requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
  2. 2023节假日及补班日期
  3. 民安汇智开展家庭医生有效签约、满意度回访工作
  4. electron-vue静默打印2019最新解决方案(含源码)
  5. 理论力学---主矢量和主矩
  6. 如何从零开始搭建智能外呼系统
  7. Neo4j-Apoc
  8. 机器学习理论学习(1)——房价预测
  9. 计算机图像处理之几何畸变矫正
  10. 免费实用的CAD移动端看图软件有它就够了!