python使用protobuf
当然首先你得安装好python和protobuf,之前的一篇博客有介绍:
https://blog.****.net/monkeycat520/article/details/81592905
接下来需要定义一个.proto文件。例如testProtobuf.proto:
syntax = "proto2";
message Person{
required int32 nID = 1;
required string sName = 2;
}
message PersonList{
repeated Person dPerson = 1;
}
在该文件夹下进入终端并使用指令:
protoc -I . --python_out=. testProtobuf.proto
执行之后会在该文件夹下产出一个testProtobuf_pb2.py文件。
-I 是指定.proto文件所在路径。
--python_out 输出生成好的pb2.py文件所在路径。
后面参数指定使用哪个.proto文件。
于是领建一个新的py文件,即可使用之前定义的proto结构进行序列化和反序列化了,例如: