This example demonstrates a simple gRPC client-server application based on the official Python quickstart guide.
The example implements a Greeter service with two RPC methods:
SayHello: Returns a greeting messageSayHelloAgain: Returns another greeting message
grpc_example.proto- Protocol Buffer definition filegrpcio-example.py- Complete client/server implementation
pip install grpcio grpcio-toolsFirst, navigate to the python-examples directory and generate the gRPC Python code:
cd python-examples
python grpcio-example.py setupThis generates:
grpc_example_pb2.py- Protocol buffer message classesgrpc_example_pb2_grpc.py- gRPC service classes
Alternatively, you can manually generate with:
cd python-examples
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. grpc_example.protoThis generates:
grpc_example_pb2.py- Protocol buffer message classesgrpc_example_pb2_grpc.py- gRPC service classes
cd python-examples
python grpcio-example.py serverOutput:
✓ gRPC Server started on port 50051
Waiting for client connections...
Press Ctrl+C to stop
cd python-examples
python grpcio-example.py clientOutput:
Connecting to gRPC server at localhost:50051...
1. Calling SayHello RPC...
✓ Client received: Hello, World!
2. Calling SayHelloAgain RPC...
✓ Client received: Hello again, gRPC User!
✓ All RPC calls completed successfully!
Start server on a different port:
python grpcio-example.py server 50052Connect client to custom port:
python grpcio-example.py client localhost 50052python grpcio-example.py setup - Generate proto files
python grpcio-example.py server [port] - Start server (default: 50051)
python grpcio-example.py client [host] [port] - Run client (default: localhost:50051)
- This example uses insecure channels for simplicity
- In production, use secure channels with TLS/SSL certificates
- The server runs indefinitely until Ctrl+C is pressed
- Each client invocation makes two RPC calls and exits