-
一、环境安装
pip install grpcio pip install grpcio-tools
-
二、编写 hello.proto文件
syntax = "proto3"; option go_package="./;hello_grpc"; package hello_grpc; // 生成文件的名字 service Greeter { rpc SayHello(HelloRequest)returns(HelloReply) {} } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }
生成python 的grpc 文件
python -m grpc_tools.protoc -I . --python_out=. --grpc_python_out=. hello.proto
会生成 如图两个文件,然后解决一下python 的导包问题。
因为我的实在proto文件下生成的,所以该成下图的
from proto import hello_pb2 as hello__pb2
三、编写客户端
import grpc
from proto import hello_pb2_grpc, hello_pb2
def run():
channel = grpc.insecure_channel('localhost:8888')
stub = hello_pb2_grpc.GreeterStub(channel)
response = stub.SayHello(hello_pb2.HelloRequest(name='我是python的客户端'))
print("Greeter client received: " + response.message)
if __name__ == '__main__':
run()
调用go 的服务端如图



