前言
使用protobuf主要是兩個步驟,序列化和反序列化。
關於Proto有哪些數據類型,然后如何編寫,此處就不贅述了,百度一下有很多。
此文主要是總結,python使用protobuf的過程,如何序列化和反序列化,對不同類型的字段如何進行賦值。
序列化
下面將一一列舉各數據類型,在python中如何正確賦值。
首先,得把編譯包給導入
import test_pb2 as pb
我分為兩部分,分別為未被repeated
修飾的字段 和 被repeated
修飾后的字段
無修飾符
字符串
test.proto
message SearchService {
string type = 1;
}
創建message對象,然后賦值即可。與python中,通過類創建實例,實例.屬性
的方式進行賦值類似
search_service = pb.SearchService()
search_service.type = "request"
數字型
test.proto
message SearchService {
int32 id = 2;
}
與字符串賦值一致
search_service = pb.SearchService()
search_service.id = 1
Message
test.proto
message SearchService {
// 定義一個message類型
message SearchRequest {
string content = 1;
string keyword = 2;
}
// 類型 字段名 序號
SearchRequest searchRequest = 3;
}
我們看到在SearchService
里序號為3
的字段的類型為SearchRequest
,這是我們新定義的message
如果把message看作是一個類,那么我將其實例化,然后賦值給對應的字段,可以嗎?
ok,這是不行的,錯誤示例:
search_service = pb.SearchService()
# 實例化SearchRequest
search_request = pb.SearchService.SearchRequest()
# 為search_request內部字段賦值
search_request.content = "hello protobuf"
search_request.keyword = "mk"
# 為search_service的searchRequest字段賦值
search_service.searchRequest = search_request
不允許在協議消息對象中分配復合字段“searchRequest”。
正確示例:
import test_pb2 as pb
search_service.searchRequest.content = "hello protobuf!"
search_service.searchRequest.keyword = "mk"
如果加上之前的那個字段,那么這樣的:
import test_pb2 as pb
search_service.type = "request"
search_service.id = 1
search_service.searchRequest.content = "hello protobuf!"
search_service.searchRequest.keyword = "mk"
Enum
枚舉類型,注意一點:必須包含一個含0的字段
test.proto
syntax = "proto3";
message SearchService {
enum SearchType {
A = 0;
B = 1;
}
SearchType searchType = 4;
}
序號為4,類型為SearchType
的枚舉類,名為searchType
的字段
此處的枚舉類型,你可以看作是網頁中的單選框,只能從給定的數據中選擇一個,不選則默認為0
# 手動選擇1
search_service.searchType = 1
# 或者是根據字段名進行選擇
search_service.searchType = pb.SearchService.SearchType.A
被repeated修飾的字段
字符串或數字
test.proto
syntax = "proto3";
message SearchService {
# 修飾符 類型 字段名 序號
repeated int32 uid = 5;
}
uid
的類型是int32
,然后被repeated
修飾,即這個字段是可重復賦值的。
那么,在python中應該怎么賦值呢?
錯誤示例:
search_service.uid = 0
如果還是和之前一樣的賦值,就會報錯
AttributeError: Assignment not allowed to repeated field "uid" in protocol message object.
正確示例:
search_service.uid.append(1)
search_service.uid.append(2)
所以,你可以將被repeated
修飾的字段看作是一個空列表,往里添加值即可!
Message
test.proto
syntax = "proto3";
message SearchService {
message Second {
string type = 1;
string word = 2;
}
repeated Second seconds = 6;
}
seconds
字段是可重復的message
類型,在python中該如何賦值?
# 實例化一個second
second = search_service.Second()
# 為second對象賦值
second.type = 'abc'
second.word = 'world'
# 添加至seconds列表中
search_service.seconds.append(second)
或者,你也可以這樣
search_service.seconds.append(
search_service.Second(type='efg', word="world")
)
或者這樣:
seconds = [
search_service.Second(type='1', word="world"),
search_service.Second(type='2', word="world")
]
search_service.seconds.extend(seconds)
所以,repeated
修飾的字段,在python中,就是一個列表
Enum
test.ptoto
syntax = "proto3";
message SearchService {
enum SortOrder {
key1 = 0;
key2 = 1;
key3 = 2;
}
repeated SortOrder sortOrder = 7;
}
使用方法與之前的完全一致
sortFields = [
# 此處key1 根據關鍵詞,獲取枚舉值
search_service.SortOrder.key1,
search_service.SortOrder.key2
]
search_service.sortOrder.extend(sortFields)
現在我們已經全部賦值好了,接着就是序列化了
b = search_service.SerializeToString()
print(b)
# b'\n\x07request\x10\x01\x1a\x15\n\x0fhello protobuf!\x12\x02mk
# \x02*\x02\x01\x022\x0c\n\x03abc\x12\x05world2\x0c\n\x03efg
# \x12\x05world2\n\n\x011\x12\x05world2\n\n\x012\x12\x05world:\x02\x00\x01'
SerializeToString
Serializes the protocol message to a binary string.
序列化此協議消息為二進制串
反序列化
現在,我們是接收方,我們收到了一串二進制。
首先,我們需要使用將其反序列化,同樣使用編譯包。
search_service = pb.SearchService()
b = b'\n\x07request\x10\x01\x1a\x15\n\x0fhello protobuf!\x12\x02mk \x02*\x02\x01\x022\x0c\n\x03abc\x12\x05world2\x0c\n\x03efg\x12\x05world2\n\n\x011\x12\x05world2\n\n\x012\x12\x05world:\x02\x00\x01'
search_service.ParseFromString(b)
# 訪問屬性值
print(search_service.type) # 輸出:request
ParseFromString
解析函數
此時,search_service
就已經含有傳輸過來的全部數據了。如果你不想使用對象.屬性
的方式調用,或者想使用類似json.loads
直接轉為python中的字典,那么你可以使用protobuf_to_dict
將其轉為字典。
安裝protobuf3_to_dict`
pip install protobuf3_to_dict
# 調用
from protobuf_to_dict import protobuf_to_dict
b = b'\n\x07request\x10\x01\x1a\x15\n\x0fhello protobuf!\x12\x02mk \x02*\x02\x01\x022\x0c\n\x03abc\x12\x05world2\x0c\n\x03efg\x12\x05world2\n\n\x011\x12\x05world2\n\n\x012\x12\x05world:\x02\x00\x01'
search_service.ParseFromString(b)
# print(search_service.type)
d = protobuf_to_dict(search_service)
print(d, type(d))
# {'type': 'request', 'id': 1, 'searchRequest': {'content': 'hello protobuf!', 'keyword': 'mk'}, 'searchType': 2, 'uid': [1, 2], 'seconds': [{'type': 'abc', 'word': 'world'}, {'type': 'efg', 'word': 'world'}, {'type': '1', 'word': 'world'}, {'type': '2', 'word': 'world'}], 'sortOrder': [0, 1]} <class 'dict'>
小小嘗試
本文中例子,我做了一個接口。
接口地址: http://47.101.154.110:8000/
請求頭 | 請求體 | 請求方式 |
---|---|---|
必須指定Content-Type: application/grpc-web+proto | 序列化后的二進制 | POST |
你可以使用postman
提交數據,來查看結果
也可以使用Python發送請求
import requests
headers = {
'Content-Type': 'application/grpc-web+proto'
}
b = b'\n\x07request\x10\x01\x1a\x15\n\x0fhello protobuf!\x12\x02mk \x02*\x02\x01\x022\x0c\n\x03abc\x12\x05world2\x0c\n\x03efg\x12\x05world2\n\n\x011\x12\x05world2\n\n\x012\x12\x05world:\x02\x00\x01'
resp = requests.post('http://47.101.154.110:8000/', data=b, headers=headers)
print(resp.text)
完整的test.proto
syntax = "proto3";
message SearchService {
string type = 1;
int32 id = 2;
// 定義一個message類型
message SearchRequest {
string content = 1;
string keyword = 2;
}
// 類型 字段名 序號
SearchRequest searchRequest = 3;
enum SearchType {
A = 0;
B = 1;
}
SearchType searchType = 4;
repeated int32 uid = 5;
message Second {
string type = 1;
string word = 2;
}
repeated Second seconds = 6;
enum SortOrder {
key1 = 0;
key2 = 1;
key3 = 2;
}
repeated SortOrder sortOrder = 7;
}
完整的賦值示例
import test_pb2 as pb
from protobuf_to_dict import protobuf_to_dict
search_service = pb.SearchService()
search_service.type = "request"
search_service.id = 1
search_service.searchRequest.content = "hello protobuf!"
search_service.searchRequest.keyword = "mk"
# search_service.searchType = pb.SearchService.SearchType.A
search_service.searchType = 2
search_service.uid.append(1)
search_service.uid.append(2)
second = search_service.Second()
second.type = 'abc'
second.word = 'world'
search_service.seconds.append(second)
search_service.seconds.append(search_service.Second(type='efg', word="world"))
seconds = [
search_service.Second(type='1', word="world"),
search_service.Second(type='2', word="world")
]
search_service.seconds.extend(seconds)
sortFields = [
search_service.SortOrder.key1,
search_service.SortOrder.key2
]
search_service.sortOrder.extend(sortFields)
b = search_service.SerializeToString()
print(b)
推薦模塊
在使用編譯包時,沒有代碼提示,還有點不習慣。
這里,推薦安裝mypy-protobuf
pip install mypy-protobuf
使用方法:
在你使用protoc
命令編譯proto文件時,新增一個參數mypy-out=
,就像這樣
protoc --python_out=. --mypy-out=. test.proto
此時會生成兩個文件,並將他們拖入項目中的同一目錄
test_pb2.py
:我們需要導入使用的編譯包
test_pb2.pyi
:存根文件,在編輯器中會有代碼提示(想了解存根文件,可以看最下面的參考文章)
效果演示: