本文将从以下几点来介绍protobuf:
1.安装
2.定义protobuf消息格式
3.编译protobuf
4.读写protobuf
1.安装
2.定义protobuf消息格式
我们将要使用的示例是一个非常简单的“地址簿”应用程序,可以在文件中读取和写入人员的联系人详细信息。地址簿中的每个人都有姓名,ID,电子邮件地址和联系电话号码。
要创建地址簿应用程序,需要从.proto文件开始。.proto文件中的定义很简单:为要序列化的每个数据结构添加消息,然后为消息中的每个字段指定名称和类型。下面是.proto定义的消息文件addressbook.proto。
syntax = "proto2";
package tutorial;
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phones = 4;
}
message AddressBook {
repeated Person people = 1;
}
关于protobuf的详细解释。
3.编译protobuf
现在,有了一个.proto,需要做的下一件事就是生成需要读写的类AddressBook(包扩Person和PhoneNumber)消息。要做到这一点,需要运行protobuf编译器protoc.exe对.proto进行编译,编译命令格式如下:
protoc -I = $ SRC_DIR --python_out = $ DST_DIR $ SRC_DIR / addressbook.proto
其中,$ SRC_DIR是源目录(应用程序的源代码所在的位置 - 如果不提供值,则使用当前目录),$ DST_DIR 是目标目录(希望生成的代码在哪里,通常和$SRC_DIR相同)。
我的示例程序如下图,protoc.exe编译器和.proto文件都在同一个目录下,如果没有为protoc.exe配置环境变量,则需要在命令行窗口中切换到包含protoc.exe的文件夹(也就是下图的the_use_of_protobuf文件夹)下运行下面的命令:
protoc --python_out=./ ./addressbook.proto
然后生成
4.读写protobuf消息
-
写一条消息
现在尝试使用protobuf类。为了将个人详细信息写入地址簿文件,需要创建并填充protobuf类的实例,然后将它们写入输出流。
这是一个程序(文件名为write.py),它从数据文件中(这里的数据文件名为data.pb)读取AddressBook类,根据用户输入添加一个新Person到AddressBook中,然后再将新AddressBook写回文件。
#! /usr/bin/python
import addressbook_pb2
import sys
# This function fills in a Person message based on user input.
def PromptForAddress(person):
person.id = int(raw_input("Enter person ID number: "))
person.name = raw_input("Enter name: ")
email = raw_input("Enter email address (blank for none): ")
if email != "":
person.email = email
while True:
number = raw_input("Enter a phone number (or leave blank to finish): ")
if number == "":
break
phone_number = person.phones.add()
phone_number.number = number
type = raw_input("Is this a mobile, home, or work phone? ")
if type == "mobile":
phone_number.type = addressbook_pb2.Person.MOBILE
elif type == "home":
phone_number.type = addressbook_pb2.Person.HOME
elif type == "work":
phone_number.type = addressbook_pb2.Person.WORK
else:
print "Unknown phone type; leaving as default value."
# Main procedure: Reads the entire address book from a file,
# adds one person based on user input, then writes it back out to the same
# file.
if len(sys.argv) != 2:
print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
sys.exit(-1)
address_book = addressbook_pb2.AddressBook()
# Read the existing address book.
try:
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())
f.close()
except IOError:
print sys.argv[1] + ": Could not open file. Creating a new one."
# Add an address.
PromptForAddress(address_book.people.add())
# Write the new address book back to disk.
f = open(sys.argv[1], "wb")
f.write(address_book.SerializeToString())
f.close()
然后在命令窗口下执行以下命令:
python write.py data.pb
在弹出来的窗口随便输入两条信息。
- 读一条信息
#! /usr/bin/python
import addressbook_pb2
import sys
# Iterates though all people in the AddressBook and prints info about them.
def ListPeople(address_book):
for person in address_book.people:
print "Person ID:", person.id
print " Name:", person.name
if person.HasField('email'):
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
# Main procedure: Reads the entire address book from a file and prints all
# the information inside.
if len(sys.argv) != 2:
print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
sys.exit(-1)
address_book = addressbook_pb2.AddressBook()
# Read the existing address book.
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())
f.close()
ListPeople(address_book)
然后在命令窗口下执行以下命令:
python read.py data.pb
你可以看到输出
到此,关于protobuf的简单使用已经介绍完了。