需要下载google的工具包
pip install protobuf==3.6.1
下载protobuf编译工具 (window平台为例)
下载地址 [https://github.com/google/protobuf/releases](https://github.com/google/protobuf/releases)
编写.proto文件, 定义消息体格式, 保存为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;
}
使用 下载的protoc.exe编译工具生成python代码
protoc --python_out=./ ./location.proto
生成的文件格式如下, 文件名为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)
反序列化 数据
str = b'\x08\xb2\xfb\xd0\xe8\x05\x11\x16\x8fx\x9f\xd0\xa0>@\x19\xa8\xebv\xe3\xca\x82\\@'
from location_pb2 import SummaryLocationReport
location = SummaryLocationReport()
location.ParseFromString(str)
print(location)
输出
report_time: 1561607602
latitude: 30.628183333333332
longitude: 114.04363333333333
网友评论