Skip to content

Latest commit

 

History

History
113 lines (80 loc) · 2.55 KB

File metadata and controls

113 lines (80 loc) · 2.55 KB

gRPC Python Example

This example demonstrates a simple gRPC client-server application based on the official Python quickstart guide.

Overview

The example implements a Greeter service with two RPC methods:

  • SayHello: Returns a greeting message
  • SayHelloAgain: Returns another greeting message

Files

  • grpc_example.proto - Protocol Buffer definition file
  • grpcio-example.py - Complete client/server implementation

Requirements

pip install grpcio grpcio-tools

Setup

First, navigate to the python-examples directory and generate the gRPC Python code:

cd python-examples
python grpcio-example.py setup

This generates:

  • grpc_example_pb2.py - Protocol buffer message classes
  • grpc_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.proto

This generates:

  • grpc_example_pb2.py - Protocol buffer message classes
  • grpc_example_pb2_grpc.py - gRPC service classes

Running the Example

Terminal 1 - Start the server

cd python-examples
python grpcio-example.py server

Output:

✓ gRPC Server started on port 50051
  Waiting for client connections...
  Press Ctrl+C to stop

Terminal 2 - Run the client

cd python-examples
python grpcio-example.py client

Output:

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!

Custom Port

Start server on a different port:

python grpcio-example.py server 50052

Connect client to custom port:

python grpcio-example.py client localhost 50052

Usage

python 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)

Notes

  • 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

Learn More