一、测试helloworld

1、在https://github.com/grpc/grpc 下载example

2、用cmd打开命令行,cd到helloworld的根目录

cd E:\pythoncode\gRPC\grpc-master\grpc-master\examples\python\helloworld

然后运行 python greeter_server.py

3、再用cmd打开另一个命令行。同样cd到helloworld的根目录

然后运行python greeter_client.py

4、即可在greeter_server.py界面看到Greeter client received: Hello, you!

二、更新gRPC服务

1、对helloworld.proto进行修改后,必须运行cmd

即打开命令行,cd到helloworld.proto和helloworld文件的根目录下(如果显示No such director,则到helloworld的根目录下)

然后运行 python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto

生成两个文件:

xx_pb2.py是数据格式调用的文件,用来和 protobuf 数据进行交互

py  xx_pb2_grpc.pgrpc传输协议接口调用的文件,用来和 grpc 进行交互

2、cd到helloworld的根目录

cd E:\pythoncode\gRPC\grpc-master\grpc-master\examples\python\helloworld

然后运行 python greeter_server.py

3、再用cmd打开另一个命令行。同样cd到helloworld的根目录

然后运行python greeter_client.py

4、即可在greeter_server.py界面看到

Greeter client received: Hello, you!
Greeter client received: Hello again, you!

注意:要运行python -m grpc_tools.protoc -I../../protos --python_out=. --grpc_python_out=. ../../protos/helloworld.proto

时,目录必须为helloworld.proto或者helloworld.proto和helloworld文件的根目录下

python gRPC测试helloworld

参考:

https://grpc.io/docs/quickstart/python.html

相关文章: