Protobuf--Python

python google protobuf 使用

google protobuf由于采用二进制打包,数据量很小,又支持主流的java,c,python语言,

所以尤其适合于mobile客户端与服务器的通信。相对于xml,html,json等格式,有其独特优势

解压protobuf-2.5.0.tar.gz,里面有个python目录。

先将protoc.exe配置到环境变量中,

然后在cmd下,切换到该目录,执行

python setup.py build

python setup.py text

python setup.py install

到这里python的protobuf库就安装结束了

示例:

完成测试test.proto

message TestMsg
{
    required int32 id=1;
    required int32 time=2;
    optional string note=3;
}

protoc.exe –python_out=d:/test/ test.proto

#-*- coding:utf-8 -*-
import google.protobuf
import TestMsg_pb2
import time

#压缩
test = TestMsg_pb2()
test.id=1
test.time=int(time.time())
test.string="asdftest"
print test
test_str = test.SerializeToString()
print test_str
#解压
test1 = TestMsg_pb2()
test1.ParseFromString(test_str)
print test1

来自为知笔记(Wiz)

附件列表

时间: 2024-08-28 12:05:22

Protobuf--Python的相关文章

安装protobuf Python支持

先从https://github.com/google/protobuf/releases?after=v3.0.0-alpha-3 下载protobuf文件.可以根据需要进行选择性的下载. 下载完成后执行,解压包     tar -zxvf protobuf-2.6.1.tar.gz 解压完成后, 进入到protobuf 文件夹,进行配置,并且make.     #cd protobuf-2.6.1     #./configure     #make     #make check 如果成功

python读写protobuf

0.     前期准备 官方protobuf定义 https://code.google.com/p/protobuf/ python使用指南 https://developers.google.com/protocol-buffers/docs/pythontutorial http://blog.csdn.net/love_newzai/article/details/6906459 安装 python对protobuf的支持 wget https://protobuf.googlecode

【泡咖啡1】linux下caffe编译以及python环境配置手记

caffe是一个深度学习的库,相信搞深度学习的话,不是用这个库就是用theano吧.要想使用caffe首先第一步就是要配置好caffe的环境.在这里,我主要说的是在debian的linux环境下如何配置好caffe的库.因为python编写程序比较方便,在文章最后,我还会具体说明如何配置python环境.本文章为本人原创,部分内容整理自网络,若有不妥之处请联系本人删除.非盈利性质网站转载请在文章开头处著名本文作者:77695,来源http://www.cnblogs.com/cj695/.盈利性

ubuntu16.04 安装 caffe cuda 相关流程

不多说了,经历了很多莫名其妙的错误最后终于安装好了,直接放安装脚本: #!/bin/bash #安装时要注意有些库可能安装失败以及安装caffe有和protobuf相关错误时可能需要重新对protobuf进行make install cd /home/zw/softwares #需要事先下载对应版本的cuda sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb sudo apt-get update sudo ap

caffe安装1

affe是一个深度学习的库,相信搞深度学习的话,不是用这个库就是用theano吧.要想使用caffe首先第一步就是要配置好caffe的环境.在这里,我主要说的是在debian的linux环境下如何配置好caffe的库.因为python编写程序比较方便,在文章最后,我还会具体说明如何配置python环境.本文章为本人原创.非盈利性质网站转载请在文章开头处著名作者:77695,来源 http://www.cnblogs.com/cj695/ .盈利性质网站转载请与作者联系,联系方式在文章后面.如未联

grpc 浅谈

一:安装 pip install grpciopip install protobufpip install grpcio-tools 二: 1,定义grpc接口 message.proto syntax = "proto3"; package example; #定义包的名称,这个名称server段和client端一模一样 service Message { #Message rpc GetMessage(GetMessageRequest) returns (GetMessageR

python通过protobuf实现rpc

由于项目组现在用的rpc是基于google protobuf rpc协议实现的,所以花了点时间了解下protobuf rpc.rpc对于做分布式系统的人来说肯定不陌生,对于rpc不了解的童鞋可以自行google,这里只是做个简单的介绍.rpc的主要功能是让分布式系统的实现更为简单,为提供强大的远程调用而不损失本地调用语义的简洁性.为了实现这个目标,rpc框架需要提供一种透明调用机制让使用者不必显示区分本地调用还是远程调用.rpc架构涉及的组件如下: 客户方像调用本地方法一样去调用远程接口方法,R

python版protobuf 安装

1. 下载protobuf源代码(当前最新版本为:2.5.0) #cd /opt #wget https://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz 2. 解压,编译,安装 #tar zxvf protobuf-2.5.0.tar.gz #cd protobuf-2.5.0 #./configure #make #make check #make install 3. 继续安装protobuf的python模块(如果不用python

Windows7上配置Python Protobuf 操作步骤

1.  按照http://blog.csdn.net/fengbingchun/article/details/8183468 中步骤,首先安装Python 2.7.10: 2.  按照http://blog.csdn.net/fengbingchun/article/details/47905907 中步骤,配置.编译Protobuf: 3.  将(2)中生成的protoc.exe文件拷贝到protobuf-master/python目录下: 4.  打开cmd,切换到protobuf-mas

python protobuf序列化repeated运用

下面是proto描述文件的定义 message Person { required string name = 1; required int32 id = 2; optional string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = H