python的grpc环境安装

环境

ubuntu:bionic的docker image

docker run -it ubuntu:bionic

python的grpc环境安装

参考grpc官网:https://grpc.io/docs/languages/python/quickstart/

# apt-get install python3-pip
# pip3 install grpcio
# pip3 install grpcio-tools

Collecting grpcio
  Downloading https://files.pythonhosted.org/packages/81/5e/168a7fa23a025beed6b7daa0981ace55e394a136db3082faed7d6cba4556/grpcio-1.34.1.tar.gz (21.1MB)
Collecting grpcio-tools
  Downloading https://files.pythonhosted.org/packages/6b/9d/a5fa6506188e44393a225245c6898a5601caf6faa1e7093ed6af1b005fe3/grpcio-tools-1.34.1.tar.gz (2.1MB)
Requirement already satisfied: six>=1.5.2 in /usr/lib/python3/dist-packages (from grpcio)
Collecting protobuf<4.0dev,>=3.5.0.post1 (from grpcio-tools)
  Downloading https://files.pythonhosted.org/packages/fe/fd/247ef25f5ec5f9acecfbc98ca3c6aaf66716cf52509aca9a93583d410493/protobuf-3.14.0-cp36-cp36m-manylinux1_x86_64.whl (1.0MB)
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from grpcio-tools)
Building wheels for collected packages: grpcio, grpcio-tools
  Running setup.py bdist_wheel for grpcio: started
  Running setup.py bdist_wheel for grpcio: still running...
  Running setup.py bdist_wheel for grpcio: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/e2/60/7c/617a7c5af21a5d60c41e66ddb55f31235ec7d3f3d22d943f51
  Running setup.py bdist_wheel for grpcio-tools: started
  Running setup.py bdist_wheel for grpcio-tools: still running...
  Running setup.py bdist_wheel for grpcio-tools: finished with status 'done'
  Stored in directory: /root/.cache/pip/wheels/ee/f1/12/0bf094c3acabbb21dd495a7048e1e2bae64c8074890fc45b27
Successfully built grpcio grpcio-tools
Installing collected packages: grpcio, protobuf, grpcio-tools
Successfully installed grpcio-1.34.1 grpcio-tools-1.34.1 protobuf-3.14.0

验证

利用docker跑服务端和客户端
helloworld程序是github grpc里的示例文件,下载目录:https://github.com/grpc/grpc/tree/master/examples/python/helloworld

  1. 服务端启动
$ docker ps | grep ubuntu
be6f5b966749        ubuntu           "/bin/sh -c /bin/bash"   2 hours ago         Up 2 hours                              dazzling_margulis
$ docker exec -it be6f5b966749  sh

helloworld# ls
greeter_client.py   greeter_server.py  
helloworld# python3 greeter_server.py
  1. 客户端启动
$ docker ps | grep ubuntu
be6f5b966749        ubuntu            "/bin/sh -c /bin/bash"   2 hours ago         Up 2 hours                              dazzling_margulis
$ docker exec -it be6f5b966749  sh

helloworld# ls
greeter_client.py   greeter_server.py  
# python3 greeter_client.py
Greeter client received: Hello, you!         <<<< 通信成功!
原文地址:https://www.cnblogs.com/abc36725612/p/14285377.html