在python中使用GRPC进行通信
步骤:
- 1、装依赖
- 2、写proto文件
- 3、生成代码
- 4、写server、client
1、装依赖
pip install grpcio
pip install protobuf
pip install grpcio-tools
2、写proto
//demo.proto
syntax = "proto3";
package demo;
service Demo {
rpc demo_func (demo_request) returns (demo_reply) {}
}
message demo_request {
string name = 1;
int32 age = 2;
}
message demo_reply {
string message = 1;
int32 salary = 2;
}
3、生成通信用的代码
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. demo.proto
4、写server、client
server
# coding=utf-8
# demo_server.py
import time
from concurrent import futures
from demo_pb2 import *
from demo_pb2_grpc import *