google protobuf因为采用二进制打包,数据量很小,又支持主流的java,c,python语言,java
因此尤为适合于mobile客户端与服务器的通讯。相对于xml,html,json等格式,有其独特优点python
解压protobuf-2.5.0.tar.gz,里面有个python目录。json
先将protoc.exe配置到环境变量中,服务器
而后在cmd下,切换到该目录,执行post
python setup.py build测试
python setup.py textui
python setup.py installgoogle
到这里python的protobuf库就安装结束了spa
示例:
完成测试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