python 操作protobuf

需要下载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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,979评论 6 528
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,750评论 3 412
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 174,647评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,299评论 1 307
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,104评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,623评论 1 320
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,716评论 3 434
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,873评论 0 285
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,380评论 1 330
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,344评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,496评论 1 365
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,055评论 5 355
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,756评论 3 343
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,146评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,410评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,072评论 3 386
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,485评论 2 370

推荐阅读更多精彩内容

  • 相关知识可以参考Cocos Creator Protobuf的js版本使用protoc2 与 protoc3 区别...
    合肥黑阅读 10,805评论 0 1
  • 谷歌Protobuf目前最新3.6.1版本官方支持许多语言:C++、Java、Python、Go等等。除官方支持外...
    chend0316阅读 11,865评论 1 7
  • Protobuf是什么? 官网解释 Protobuf 是Google为序列化数据结构提供的语言无关,平台无关,可扩...
    邝健强阅读 2,551评论 0 4
  • 2016更新:本文写于2014。其中值得注意的是,建议默认使用python编写脚本,对跨平台有好处。而并非下文即将...
    DonaldW阅读 11,331评论 7 34
  • 视频格式 当前,video 元素支持三种视频格式: 格式 IE Firefox Opera Chrome ...
    原点_da4e阅读 841评论 0 0