python 中 protobuf的使用

Oct 16, 2017


###安装

首先下载对应语言版本的protobufprotobuf

到python目录,编译

python setup.py build 
python setup.py install 
python setup.py test

验证安装是否成功

protobuf-test

如果有报错,ptotoc 没找到,可以在./src下编译protoc,或者直接下载protoc

###使用

syntax = "proto3";
message hx
{
	string name = 1;
	uint32 age = 2;
	string sex = 3;
	string address = 4;
	
}


D:\python\protobuf-3.3.0\src>protoc -I=../result --python_out=../result ../resul
t/hx.proto

其中 -I是源文件目录 –python_out是文件输出目录 最后的参数addressbook.proto是你需要编译的协议文件,

编译好之后你就会在目标目录里面看到输出的结果文件,hx_pb2.py文件

导入生成的文件并使用

from proto import hx_pb2

info = hx_pb2.InfoRequest()
info.ParseFromString(byteArray.ReadBytes())