python下grpc與protobuf的編寫使用示例
更新時間:2022年04月12日 09:22:51 作者:Jeff的技術棧
這篇文章主要為大家介紹了python下grpc與protobuf的編寫使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
1. python下protobuf使用
1.1 安裝protobuf
pip3.6 install grpcio #安裝grpc pip3.6 install grpcio-tools #安裝grpc tools
1.2 protobuf3定義格式
新建protobuf文件名:hello.proto
syntax = "proto3";
message HelloRequest {
string name = 1;
}
1.3 生成proto的python文件
cd hello.proto文件路徑下 命令:python3.6 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto 注意:只有python生成兩個文件 命令解釋: python3.6 -m grpc_tools.protoc:實際需要使用grpc_tools.protoc這里面的命令 --python_out=. :生成的python文件放在當前路徑下,這是給rpc用的文件 --grpc_python_out=. :生成的python文件放在當前路徑下,這是給grpc用的文件 -I.:指import,當前目錄下找hello.proto文件

1.4 對比一下protobuf生成的效果
res.SerializeToString() # 序列化二進制
res2.ParseFromString(res_str) # 反序列化二進制
import json
from python_grpc.proto import hello_pb2
def main():
res = hello_pb2.HelloRequest()
res.name = "jeff"
res_str = res.SerializeToString() # 序列化二進制
print(res_str) # b'\n\x04jeff'
print(len((res_str))) # 6,和json對比,josn長度為:16
res2 = hello_pb2.HelloRequest()
res2.ParseFromString(res_str) # 反序列化二進制
print(res2.name) # jeff
res_json = {
"name":"jeff"
}
print(len(json.dumps(res_json))) # 16,json和proto壓縮對比,proto壓縮后:6
if __name__ == '__main__':
main()
2.python下grpc使用
2.1編寫hello.proto文件
syntax = "proto3";
package services;
option go_package = "./;proto";
service Greeter {
// 定義SayHello方法
rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
string name = 1; //編號
}
message HelloReply {
string message = 1; //編號
}
2.2 生成proto的python文件
cd hello.proto文件路徑下 命令:python3.6 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto 注意:只有python生成兩個文件 命令解釋: python3.6 -m grpc_tools.protoc:實際需要使用grpc_tools.protoc這里面的命令 --python_out=. :生成的python文件放在當前路徑下,這是給rpc用的文件 --grpc_python_out=. :生成的python文件放在當前路徑下,這是給grpc用的文件 -I.:指import,當前目錄下找hello.proto文件 注意:生成的*_grpc.py文件的導包需要修改,否則報錯:要讓python找到hello_pb2 import hello_pb2 as hello__pb2 改為: from python_grpc.proto import hello_pb2 as hello__pb2
2.3 編寫server端
from concurrent import futures
import grpc
from python_grpc.proto import hello_pb2, hello_pb2_grpc
# 業(yè)務處理
class Greeter(hello_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return hello_pb2.HelloReply(message=f"你好,{request.name}")
# 啟動
def start():
# 1.實例化server
Thread = futures.ThreadPoolExecutor(max_workers=10) ## 設置線程池,并發(fā)大小
server = grpc.server(Thread)
# 2.注冊邏輯到server中
hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
# 3.啟動server
server.add_insecure_port("127.0.0.1:8888")
server.start()
server.wait_for_termination()
if __name__ == '__main__':
start()
2.4 編寫cilent端
import grpc
from python_grpc.proto import hello_pb2, hello_pb2_grpc
# rpc調用
def main():
# 這里使用with,調用完會自動關閉。優(yōu)雅寫法
with grpc.insecure_channel("127.0.0.1:8888") as channel:
stub = hello_pb2_grpc.GreeterStub(channel)
# 調用定義的SayHello方法
rep = stub.SayHello(
hello_pb2.HelloRequest(name="jeff") # 傳遞定義的HelloRequest類型參數(shù)
)
return rep
# 業(yè)務代碼
def start():
rep = main()
print(rep.message) # 你好,jeff
if __name__ == '__main__':
start()以上就是python下grpc與protobuf的編寫使用的詳細內容,更多關于python grpc與protobuf編寫使用的資料請關注腳本之家其它相關文章!
相關文章
Python實現(xiàn)監(jiān)控鍵盤鼠標操作示例【基于pyHook與pythoncom模塊】
這篇文章主要介紹了Python實現(xiàn)監(jiān)控鍵盤鼠標操作,結合實例形式分析了Python基于pyHook與pythoncom模塊的鍵盤、鼠標事件響應及日志文件操作相關實現(xiàn)技巧,需要的朋友可以參考下2018-09-09
Python請求庫發(fā)送HTTP POST請求的示例代碼
這段代碼使用了Python的requests庫來發(fā)送HTTP POST請求,向本地服務器的API發(fā)送數(shù)據(jù),并處理響應,一步步解釋這個代碼2024-08-08
從入門到精通:Python項目打包與setup.py實戰(zhàn)指南
想要將你的Python項目分享給世界嗎?本指南將帶你從零開始,一步步學習如何打包你的Python項目,并創(chuàng)建一個專業(yè)的setup.py文件,我們將分享實用的技巧和最佳實踐,幫助你的項目在Python社區(qū)中脫穎而出,跟著我們的步伐,讓你的項目打包變得輕松有趣!2024-03-03
Python?ModuleNotFoundError:?No?module?named?‘xxx‘可能的解決方
本文主要介紹了Python?ModuleNotFoundError:?No?module?named?‘xxx‘可能的解決方案大全,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧Chat?Gpt<BR>2023-07-07

