gRPC Python Tutorial¶
This tutorial aims to provide a quick introduction on how to develop a simple application by using gRPC ecosystem using Python. Even though we implement a client-server application here, you will have a good view on the key concepts of the gRPC.
Overview¶
We will implement a gRPC service called SystemInfo
that is used to retrieve various system
information, for example product name, distro info, kernel version, CPU usage and a
client application that invokes the service developed.
In a nutshell, developing a gRPC service consists of the following steps:
- Service definition in a
.proto
file - Code generation from the service definition
- Implementing the server-side logic of this service definition
- Implementing the client-side logic of this service definition
Prerequisities¶
Python 2.7
,Python 3.4
or higherpip
version 9.0.1 or higher
Let's assume Python3.7
is installed on the system.
Install gRPC for Python¶
sudo python3.7 -m pip install grpcio
gRPC tools for Python:¶
Python’s gRPC tools include the protocol buffer compiler protoc
and the special plugin for generating server and client code from .proto
service definitions.
To install gRPC tools, run the following command:
sudo python3.7 -m pip install grpcio-tools
Getting the Example Code¶
Download the tutorials from the following link:
Then extract the archive:
tar xzvf tutorials.tar.gz
Generate gRPC code¶
You need to generate gRPC code for the client and server.
Go to python-samples
project directory.
cd python-samples/systemservice
For client:
python3.7 -m grpc_tools.protoc -I=api --python_out=./client/ --grpc_python_out=./client/ api/System.proto
This generates System_pb2.py
, which contains our generated request and response classes, and System_pb2_grpc.py
which contains our generated client and server classes. Generated files can be found under the client
folder.
For Server:
python3.7 -m grpc_tools.protoc -I=api --python_out=server/ --grpc_python_out=server/ api/System.proto
This generates System_pb2.py
, which contains our generated request and response classes, and System_pb2_grpc.py
which contains our generated client and server classes. Generated files can be found under the server
folder.
RUNNING CLIENT AND SERVER¶
NOTICE
You need to run the server and client with root privileges which means install the required libraries, that are specified in the Prerequisites section, with a root privileged user or with the root user.
First Run:
sudo python3.7 server/server_main.py unix /tmp/local.sock
and then
sudo python3.7 client/client_main.py unix /tmp/local.sock
Output:
[grpc-client] Product : VirtualBox
[grpc-client] Distro : debian "10" buster
[grpc-client] Kernel : GNU/Linux 4.19.0-11-amd64
[grpc-client] Board : Oracle Corporation VirtualBox Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
[grpc-client] CPU : Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz ,load: 100
[grpc-client] Memory : 9.9G total, 1.7G used, 6.7G free, 7.8G avail
[grpc-client] Disk : VBOX HARDDISK /dev/sda 200G 1.7G 7.8G 0.5
The server creates the /tmp/local.sock
unix domain socket and waits for connection. Client connects to the server via the same socket.
Development¶
Project Structure¶
Before proceeding, make your development environment ready with the Prerequisites section.
- First, you must create your own
.proto
file - Compile
.proto
and generate a code - Create your
client
and/orserver
by using the generated code - Run your program
The project structure should look like as:
python-samples
└── systemservice
├── api
│ └── System.proto
├── client
│ └── client_main.py
├── README.md
└── server
├── helper.py
└── server_main.py
4 directories, 6 files
api
directory contains our.proto
fileclient
directory contains our gRPC client implementationserver
directory contains our gRPC server implementation (server_main.py) and the backend logic (helper.py)