protobuf
来源:互联网 发布:淘宝服装推广方案 编辑:程序博客网 时间:2024/05/22 00:30
Overview
这一节给出Python代码使用protobuf的例子。
proto file
使用之前的示例消息结构。
/*protoc -I=. --cpp_out=. helloworld.proto protoc -I=. --python_out=. helloworld.proto */message helloworld { required int32 id = 1; // ID required string str = 2; // str optional int32 opt = 3; //optional field }
compile proto file
protoc -I=. --python_out=. helloworld.proto
Writer
#!/usr/bin/python'''$ chmod +x writer.py$ ./writer.py DONE.$ hexdump -C log_py00000000 08 65 12 05 68 65 6c 6c 6f |.e..hello|00000009$ '''import helloworld_pb2msg = helloworld_pb2.helloworld()msg.id = 101msg.str = "hello"f = open("./log_py", "wb")f.write(msg.SerializeToString())f.close()print "DONE."
Reader
#!/usr/bin/python'''$ chmod +x reader.py $ ./reader.py 101helloDONE.$'''import helloworld_pb2msg = helloworld_pb2.helloworld() f = open("./log_py", "rb")msg.ParseFromString(f.read())f.close()print msg.idprint msg.strprint "DONE."
阅读全文
0 0
- protobuf
- ProtoBuf
- protobuf
- protobuf
- protobuf
- Protobuf
- protobuf
- Protobuf
- protobuf
- protobuf
- protobuf
- protobuf
- protobuf
- protobuf
- ProtoBuf
- 【Protobuf】
- 【Protobuf】
- protobuf
- 1.uC/OS III 内核, 你应该知道的东西
- 感知机
- C#中out和ref之间的区别
- Android将camera获取到的YuvData在jni中转化为Mat
- basemap构建地图
- protobuf
- 每日一题——判断二叉树是否平衡,求一棵二叉树的镜像
- 方法内部类
- 小结 | 函数的调用过程(栈帧)
- 安卓工作室android studio 美化 ,设置背景图片。
- Hdu6098 Inversion(2017多校第6场)
- spring mvc基础篇(十二):综合案例一
- 8.10抽象类与接口
- CentOS下安装Python3后BeautifulSoup 版本不兼容问题解决方法