protobuf安装使用(python版)

安装

protobuf源代码(各种语言实现) 
https://github.com/google/protobuf 
我们只需要python版和protoc(编译proto到各个语言)

1、到Python目录,编译 
python setup.py build 
python setup.py install 
python setup.py test

2、验证安装是否成功

  #python 
  >>>import google.protobuf 

如果报错,检查错误。如果是protoc没找到,可以到./src下编译出protoc,或者直接下载protoc-3.2.0-win32 release版,把protoc被放到./src. 
ref : http://www.tuicool.com/articles/VfQfM3

使用

编写完proto文件,直接使用protoc生成logic_pb2.py文件

protoc --python_out=. logic.proto

导入生成的py文件并使用:

 from proto import logic_pb2

 login = logic_pb2.LoginRequest()
 login.ParseFromString(byteArray.ReadBytes())

lua版protobuf 参见protoc-gen-lua:https://github.com/sean-lin/protoc-gen-lua

猜你喜欢

转载自blog.csdn.net/lzl759552889/article/details/84646226