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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章