python下protobuf体验

 

安装

python -m pip install grpcio -i https://pypi.doubanio.com/simple #安装grpc
python -m pip install grpcio-tools -i https://pypi.doubanio.com/simple #安装grpc tools

编写protobuf3文件

syntax = "proto3";

message HelloRequest {
string name = 1; //1是编号,不是值
}

生成proto的python文件

# cd proto文件目录
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto

查看protobuf生成的代码

from grpc_test import helloworld_pb2

request = helloworld_pb2.HelloRequest()

request.name = "bobby"
req_str = request.SerializeToString()
print(req_str)

request2 = helloworld_pb2.HelloRequest()

request2.ParseFromString(req_str)
print(request2.name)

Comments

Popular posts from this blog

微服务科普

ESXi添加USB硬盘作为数据存储

油焖大虾菜谱