Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions python-examples/GRPC_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@ pip install grpcio grpcio-tools

## Setup

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

```bash
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto
cd python-examples
python grpcio-example.py setup
```

Or use the built-in setup command:
This generates:
- `grpc_example_pb2.py` - Protocol buffer message classes
- `grpc_example_pb2_grpc.py` - gRPC service classes

Alternatively, you can manually generate with:

```bash
cd python-examples
python grpcio-example.py setup
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. grpc_example.proto
```

This generates:
Expand Down
68 changes: 44 additions & 24 deletions python-examples/grpcio-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
pip install grpcio grpcio-tools

Setup:
Before running, generate the gRPC Python code from the proto file:
python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto
Navigate to the python-examples directory and run setup:
cd python-examples
python grpcio-example.py setup

To run:
1. Start the server: python grpcio-example.py server
2. In another terminal, run the client: python grpcio-example.py client
cd python-examples
1. Terminal 1: python grpcio-example.py server
2. Terminal 2: python grpcio-example.py client

Note: This example uses insecure channels for simplicity. In production,
use secure channels with TLS/SSL certificates.
Expand All @@ -28,35 +30,43 @@
import time
import subprocess

# Check if proto files are generated, if not, generate them
if not os.path.exists('python-examples/grpc_example_pb2.py'):
print("Generating gRPC Python code from proto file...")
# Check if proto files are generated in the same directory
script_dir = os.path.dirname(os.path.abspath(__file__))
proto_file = os.path.join(script_dir, 'grpc_example_pb2.py')

if not os.path.exists(proto_file):
print("gRPC proto files not found. Attempting to generate...")
proto_source = os.path.join(script_dir, 'grpc_example.proto')

if not os.path.exists(proto_source):
print(f"Error: Proto file not found at {proto_source}")
sys.exit(1)

try:
subprocess.run([
sys.executable, '-m', 'grpc_tools.protoc',
'-I.', '--python_out=.', '--grpc_python_out=.',
'python-examples/grpc_example.proto'
], check=True)
f'-I{script_dir}',
f'--python_out={script_dir}',
f'--grpc_python_out={script_dir}',
proto_source
], check=True, cwd=script_dir)
print("Generated successfully!\n")
except subprocess.CalledProcessError as e:
print(f"Error generating proto files: {e}")
print("Make sure grpcio-tools is installed: pip install grpcio-tools")
sys.exit(1)

# Import generated proto classes
# Note: proto files should be generated in the same directory as this script
try:
from python-examples import grpc_example_pb2
from python-examples import grpc_example_pb2_grpc
import grpc_example_pb2
import grpc_example_pb2_grpc
except ImportError:
# Try alternative import path
try:
sys.path.insert(0, 'python-examples')
import grpc_example_pb2
import grpc_example_pb2_grpc
except ImportError:
print("Error: Failed to import generated gRPC modules.")
print("Please ensure the proto files are generated correctly.")
sys.exit(1)
print("Error: gRPC proto files not found.")
print("Please run: python grpcio-example.py setup")
print("Or manually generate with:")
print(" python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto")
sys.exit(1)


# Server implementation
Expand Down Expand Up @@ -171,13 +181,23 @@ def print_usage():

if command == 'setup':
print("Generating gRPC Python code from proto file...")
script_dir = os.path.dirname(os.path.abspath(__file__))
proto_source = os.path.join(script_dir, 'grpc_example.proto')

if not os.path.exists(proto_source):
print(f"✗ Error: Proto file not found at {proto_source}")
sys.exit(1)

try:
subprocess.run([
sys.executable, '-m', 'grpc_tools.protoc',
'-I.', '--python_out=.', '--grpc_python_out=.',
'python-examples/grpc_example.proto'
], check=True)
f'-I{script_dir}',
f'--python_out={script_dir}',
f'--grpc_python_out={script_dir}',
proto_source
], check=True, cwd=script_dir)
print("✓ Generated successfully!")
print(f"✓ Files created in: {script_dir}")
print("\nYou can now run:")
print(" python grpcio-example.py server")
print(" python grpcio-example.py client")
Expand Down