步骤:
1.下载protoc(protocol compiler)
2.编写.proto文件
3.编译(生成xxxx_pb2.py)
4.引用xxxx_pb2.py
----------------------------------------------------------
详解:
【下载protoc】
https://github.com/google/protobuf/releases
根据自己的平台下载对应的编译器,我的是win10-64位:
解压到C:/Program Files下面:
设置环境变量:这一步使你在本地任何地方使用protoc这个指令
(右击“此电脑”。。。)
测试protoc:
新打开一个命令行:输入protoc --version,如果将输出版本号,说明protoc安装好了
【编写.proto协议文件】
新建一个hello_protobuf文件夹,再在里面建一个空的protobuf文件夹(专门用于存放协议文件):
用pycharm打开该文件夹:
效果:
新建协议文件:
在hello_protobuf/protobuf/目录下手动创建addressbook.proto文件:
并在addressbook.proto中输入:
syntax = "proto3";
message Person {
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
string number = 1;
PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
message AddressBook {
repeated Person people = 1;
}
【编译】:
打开命令行,CD到hello_protobuf/protobuf/下面,执行protoc --python_out=./ addressbook.proto
没有输出说明未出错。
新生成hello_protobuf/protobuff/addressbook_pb2.py——这是我们python最终要用的协议文件了
【写测试】:
输入addressbook_test_writing.py
在该文件中输入:
from protobuf import addressbook_pb2
def PromptForAddress(person):
person.id = 1
person.name = "mc.meng"
person.email = "menghaocheng@qq.com"
phone_number = person.phones.add()
phone_number.number = "18565772445"
phone_number.type = addressbook_pb2.Person.MOBILE
def write_test():
address_book = addressbook_pb2.AddressBook()
address_book_file = "./data/addressbook.txt"
try:
f = open(address_book_file, "rb")
address_book.ParseFromString(f.read())
f.close()
except IOError:
print(address_book_file + ": Could not open file. Creating a new one.")
PromptForAddress(address_book.people.add())
f = open(address_book_file, "wb")
f.write(address_book.SerializeToString())
f.close()
if __name__ == "__main__":
write_test()
在创建hello_protobuf/data/目录用于存放数据:
在pycharm中按F5运行:
将输出:
Connected to pydev debugger (build 172.3968.37)
./data/addressbook.txt: Could not open file. Creating a new one.
Process finished with exit code 0
此时在hello_protobuf/data/下生成 了addressbook.txt文件
【读测试】:
用相同的方法合建address_test_reading.py,然后输入以下内容:
import protobuf.addressbook_pb2 as addressbook_pb2
def ListPeople(address_book):
for person in address_book.people:
print("Person ID:", person.id)
print(" Name:", person.name)
print(" E-mail address:", person.email)
for phone_number in person.phones:
if phone_number.type == addressbook_pb2.Person.MOBILE:
print(" Mobile phone #: ")
elif phone_number.type == addressbook_pb2.Person.HOME:
print(" Home phone #: ")
elif phone_number.type == addressbook_pb2.Person.WORK:
print(" Work phone #: ")
print(phone_number.number)
def read_test():
address_book = addressbook_pb2.AddressBook()
address_book_file = "./data/addressbook.txt"
f = open(address_book_file, "rb")
address_book.ParseFromString(f.read())
f.close()
ListPeople(address_book)
if __name__ == "__main__":
read_test()
按F5运行,将输出:
Connected to pydev debugger (build 172.3968.37)
Person ID: 1
Name: mc.meng
E-mail address: menghaocheng@qq.com
Mobile phone #:
18565772445
Process finished with exit code 0