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中
发布了237 篇原创文章 · 获赞 98 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/KingOfMyHeart/article/details/102881900