python序列化和反序列化pb消息

参考链接:https://www.jianshu.com/p/091b99beb6bc

1.编辑location.proto文件

syntax = "proto2";
package location;
message SummaryLocationReport
{
  required int64 report_time = 1;
  required double latitude = 2;
  required double longitude = 3;
  optional int32 mileage = 4;
}

2.使用 下载的protoc.exe编译工具生成python代码

protoc --python_out=./ ./location.proto

3.生成的文件格式如下, 文件名为location_pb2

# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: location.proto

import sys

_b = sys.version_info[0] < 3 and (lambda x: x) or (lambda x: x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database

# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()

DESCRIPTOR = _descriptor.FileDescriptor(
    name='location.proto',
    package='location',
    syntax='proto2',
    serialized_options=None,
    serialized_pb=_b(
        '\n\x0elocation.proto\x12\x08location\"b\n\x15SummaryLocationReport\x12\x13\n\x0breport_time\x18\x01 \x02(\x03\x12\x10\n\x08latitude\x18\x02 \x02(\x01\x12\x11\n\tlongitude\x18\x03 \x02(\x01\x12\x0f\n\x07mileage\x18\x04 \x01(\x05')
)

_SUMMARYLOCATIONREPORT = _descriptor.Descriptor(
    name='SummaryLocationReport',
    full_name='location.SummaryLocationReport',
    filename=None,
    file=DESCRIPTOR,
    containing_type=None,
    fields=[
        _descriptor.FieldDescriptor(
            name='report_time', full_name='location.SummaryLocationReport.report_time', index=0,
            number=1, type=3, cpp_type=2, label=2,
            has_default_value=False, default_value=0,
            message_type=None, enum_type=None, containing_type=None,
            is_extension=False, extension_scope=None,
            serialized_options=None, file=DESCRIPTOR),
        _descriptor.FieldDescriptor(
            name='latitude', full_name='location.SummaryLocationReport.latitude', index=1,
            number=2, type=1, cpp_type=5, label=2,
            has_default_value=False, default_value=float(0),
            message_type=None, enum_type=None, containing_type=None,
            is_extension=False, extension_scope=None,
            serialized_options=None, file=DESCRIPTOR),
        _descriptor.FieldDescriptor(
            name='longitude', full_name='location.SummaryLocationReport.longitude', index=2,
            number=3, type=1, cpp_type=5, label=2,
            has_default_value=False, default_value=float(0),
            message_type=None, enum_type=None, containing_type=None,
            is_extension=False, extension_scope=None,
            serialized_options=None, file=DESCRIPTOR),
        _descriptor.FieldDescriptor(
            name='mileage', full_name='location.SummaryLocationReport.mileage', index=3,
            number=4, type=5, cpp_type=1, label=1,
            has_default_value=False, default_value=0,
            message_type=None, enum_type=None, containing_type=None,
            is_extension=False, extension_scope=None,
            serialized_options=None, file=DESCRIPTOR),
    ],
    extensions=[
    ],
    nested_types=[],
    enum_types=[
    ],
    serialized_options=None,
    is_extendable=False,
    syntax='proto2',
    extension_ranges=[],
    oneofs=[
    ],
    serialized_start=28,
    serialized_end=126,
)

DESCRIPTOR.message_types_by_name['SummaryLocationReport'] = _SUMMARYLOCATIONREPORT
_sym_db.RegisterFileDescriptor(DESCRIPTOR)

SummaryLocationReport = _reflection.GeneratedProtocolMessageType('SummaryLocationReport', (_message.Message,), {
    'DESCRIPTOR': _SUMMARYLOCATIONREPORT,
    '__module__': 'location_pb2'
    # @@protoc_insertion_point(class_scope:location.SummaryLocationReport)
})
_sym_db.RegisterMessage(SummaryLocationReport)


# @@protoc_insertion_point(module_scope)

4.反序列化pb

from location_pb2 import SummaryLocationReport
location = SummaryLocationReport()
location.ParseFromString(str)

print(location)
#反序列化的数据保存在对象location中
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Protobuf(Protocol Buffers)是一种由Google开发的高效的序列化反序列化技术。它可以将结构化数据转换为紧凑且高效的二进制格式,以便在不同的系统之间进行数据交换和存储。下面是Protobuf序列化反序列化的介绍和示例: 1. 定义消息结构:首先,我们需要定义消息的结构,即使用Protobuf的语法来定义消息的字段和类型。例如,我们可以定义一个名为Person的消息,其中包含姓名和年龄两个字段: ```protobuf syntax = "proto3"; message Person { string name = 1; int32 age = 2; } ``` 2. 编译消息定义:接下来,我们需要使用Protobuf编译器将消息定义编译成相应的代码文件。例如,使用protoc命令将上述消息定义编译成Python代码: ```shell protoc --python_out=. person.proto ``` 3. 序列化:在发送方,我们可以使用生成的代码将消息对象序列化为二进制数据。例如,在Python中,我们可以使用生成的代码创建Person对象,并将其序列化为字节串: ```python from person_pb2 import Person person = Person() person.name = "Alice" person.age = 25 serialized_data = person.SerializeToString() ``` 4. 反序列化:在接收方,我们可以使用生成的代码将接收到的二进制数据反序列化消息对象。例如,在Python中,我们可以使用生成的代码将字节串反序列化为Person对象: ```python from person_pb2 import Person received_data = b'\n\x05Alice\x10\x19' person = Person() person.ParseFromString(received_data) print(person.name) # 输出:Alice print(person.age) # 输出:25 ``` 通过使用Protobuf进行序列化反序列化,我们可以实现高效的数据交换和存储,同时减少网络传输和磁盘空间的占用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值