-
-
protobuf基础快速入门
-
发表于: 2021-1-22 22:30 2989
-
github地址:https://github.com/protocolbuffers/protobuf 完成相关配置:下载工具包,pip安装protobuf模块 protobuf正向开发流程: 1.编写addressbook.proto文件 2.用protoc.exe编译输出addressbook_pb2.py文件: protoc --python_out=. addressbook.proto 3.另写一个demo.py,导入addressbook_pb2.py,编写序列化和反序列化代码,运行输出
基于官方示例,加入个人认为常用到的
以下为 addressbook.proto 文件
syntax = "proto3";
package aaa;
message AddressBook {
repeated Person people = 1;
}
message Person {
string name = 1;
int32 id = 2;
string email = 3;
repeated PhoneNumber phones = 4;
repeated Money money = 5;
bool isMan = 6;
Mapsss maps = 7;
repeated int32 nums = 8;
repeated string strs = 9;
bytes byte=10;
message Money {
float ch = 1;
float us = 2;
float uk = 3;
float ja=4;
}
enum PhoneType1111 {
MOBILE11 = 0;
HOME11 = 1;
WORK11 = 2;
}
}
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
message Mapsss {
map<int32, int32> mapint = 1;
map<string, string> mapstr = 2;
}
以下为 demo.py 文件
#!/usr/bin/env python # -*- coding: UTF-8 -*- import addressbook_pb2 addressBook = addressbook_pb2.AddressBook() def Serialize_To_String(): person = addressBook.people.add() person.id = 111 person.name = '看雪论坛' person.email = '00000001314250000@qq.com' person.isMan = True person.nums.append(1) person.nums.extend([2,3]) person.strs.append('1111') person.strs.extend(['2222', '3333']) person.byte=bytes('嘻嘻哈哈',encoding='utf-8') phoneNumber = person.phones.add() phoneNumber.number = '12300000322222' phoneNumber.type = addressbook_pb2.Person.MOBILE11 # # 效果等于上 3 行 # person.phones.add(number='12300000322222', type=addressbook_pb2.Person.MOBILE11) person.money.add(ch=13.14,us=5.2,uk=2.5,ja=6.23) maps = person.maps maps.mapint[4444] = 5555 maps.mapint[6666] = 7777 maps.mapint[8888] = 9999 maps.mapstr['str1']='str111111111' maps.mapstr['str2']='str222222222' SerializeToString = addressBook.SerializeToString() print(SerializeToString) with open('addressBook.bin', 'wb') as f: f.write(SerializeToString) return SerializeToString def Parse_From_String(SerializeToString): # with open('addressBook.bin', mode='rb') as f: # addressBook.ParseFromString(f.read()) addressBook.ParseFromString(SerializeToString) print('MOBILE:', addressbook_pb2.MOBILE) for person in addressBook.people: print('id:', person.id) print('name:', person.name) print('email:', person.email) print('isMan:', person.isMan) print('nums:', person.nums) print('strs:', person.strs) print('byte:', person.byte.decode()) for pn in person.phones: print('number:', pn.number) print('type(MOBILE11):', pn.type) for my in person.money: print('ch:', my.ch) print('us:', my.us) print('uk:', my.uk) print('ja:', my.ja) mapint = person.maps.mapint for key in mapint: print(key, mapint[key]) mapstr = person.maps.mapstr for key in mapstr: print(key, mapstr[key]) if __name__ == "__main__": # 序列化 serializeToString = Serialize_To_String() print('========================================\n========================================') # 反序列化 Parse_From_String(serializeToString)
[招生]系统0day安全班,企业级设备固件漏洞挖掘,Linux平台漏洞挖掘!