|
10 | 10 | pip install grpcio grpcio-tools |
11 | 11 |
|
12 | 12 | Setup: |
13 | | - Before running, generate the gRPC Python code from the proto file: |
14 | | - python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto |
| 13 | + Navigate to the python-examples directory and run setup: |
| 14 | + cd python-examples |
| 15 | + python grpcio-example.py setup |
15 | 16 |
|
16 | 17 | To run: |
17 | | - 1. Start the server: python grpcio-example.py server |
18 | | - 2. In another terminal, run the client: python grpcio-example.py client |
| 18 | + cd python-examples |
| 19 | + 1. Terminal 1: python grpcio-example.py server |
| 20 | + 2. Terminal 2: python grpcio-example.py client |
19 | 21 |
|
20 | 22 | Note: This example uses insecure channels for simplicity. In production, |
21 | 23 | use secure channels with TLS/SSL certificates. |
|
28 | 30 | import time |
29 | 31 | import subprocess |
30 | 32 |
|
31 | | -# Check if proto files are generated, if not, generate them |
32 | | -if not os.path.exists('python-examples/grpc_example_pb2.py'): |
33 | | - print("Generating gRPC Python code from proto file...") |
| 33 | +# Check if proto files are generated in the same directory |
| 34 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 35 | +proto_file = os.path.join(script_dir, 'grpc_example_pb2.py') |
| 36 | + |
| 37 | +if not os.path.exists(proto_file): |
| 38 | + print("gRPC proto files not found. Attempting to generate...") |
| 39 | + proto_source = os.path.join(script_dir, 'grpc_example.proto') |
| 40 | + |
| 41 | + if not os.path.exists(proto_source): |
| 42 | + print(f"Error: Proto file not found at {proto_source}") |
| 43 | + sys.exit(1) |
| 44 | + |
34 | 45 | try: |
35 | 46 | subprocess.run([ |
36 | 47 | sys.executable, '-m', 'grpc_tools.protoc', |
37 | | - '-I.', '--python_out=.', '--grpc_python_out=.', |
38 | | - 'python-examples/grpc_example.proto' |
39 | | - ], check=True) |
| 48 | + f'-I{script_dir}', |
| 49 | + f'--python_out={script_dir}', |
| 50 | + f'--grpc_python_out={script_dir}', |
| 51 | + proto_source |
| 52 | + ], check=True, cwd=script_dir) |
40 | 53 | print("Generated successfully!\n") |
41 | 54 | except subprocess.CalledProcessError as e: |
42 | 55 | print(f"Error generating proto files: {e}") |
43 | 56 | print("Make sure grpcio-tools is installed: pip install grpcio-tools") |
44 | 57 | sys.exit(1) |
45 | 58 |
|
46 | 59 | # Import generated proto classes |
| 60 | +# Note: proto files should be generated in the same directory as this script |
47 | 61 | try: |
48 | | - from python-examples import grpc_example_pb2 |
49 | | - from python-examples import grpc_example_pb2_grpc |
| 62 | + import grpc_example_pb2 |
| 63 | + import grpc_example_pb2_grpc |
50 | 64 | except ImportError: |
51 | | - # Try alternative import path |
52 | | - try: |
53 | | - sys.path.insert(0, 'python-examples') |
54 | | - import grpc_example_pb2 |
55 | | - import grpc_example_pb2_grpc |
56 | | - except ImportError: |
57 | | - print("Error: Failed to import generated gRPC modules.") |
58 | | - print("Please ensure the proto files are generated correctly.") |
59 | | - sys.exit(1) |
| 65 | + print("Error: gRPC proto files not found.") |
| 66 | + print("Please run: python grpcio-example.py setup") |
| 67 | + print("Or manually generate with:") |
| 68 | + print(" python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. python-examples/grpc_example.proto") |
| 69 | + sys.exit(1) |
60 | 70 |
|
61 | 71 |
|
62 | 72 | # Server implementation |
@@ -171,13 +181,23 @@ def print_usage(): |
171 | 181 |
|
172 | 182 | if command == 'setup': |
173 | 183 | print("Generating gRPC Python code from proto file...") |
| 184 | + script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 185 | + proto_source = os.path.join(script_dir, 'grpc_example.proto') |
| 186 | + |
| 187 | + if not os.path.exists(proto_source): |
| 188 | + print(f"✗ Error: Proto file not found at {proto_source}") |
| 189 | + sys.exit(1) |
| 190 | + |
174 | 191 | try: |
175 | 192 | subprocess.run([ |
176 | 193 | sys.executable, '-m', 'grpc_tools.protoc', |
177 | | - '-I.', '--python_out=.', '--grpc_python_out=.', |
178 | | - 'python-examples/grpc_example.proto' |
179 | | - ], check=True) |
| 194 | + f'-I{script_dir}', |
| 195 | + f'--python_out={script_dir}', |
| 196 | + f'--grpc_python_out={script_dir}', |
| 197 | + proto_source |
| 198 | + ], check=True, cwd=script_dir) |
180 | 199 | print("✓ Generated successfully!") |
| 200 | + print(f"✓ Files created in: {script_dir}") |
181 | 201 | print("\nYou can now run:") |
182 | 202 | print(" python grpcio-example.py server") |
183 | 203 | print(" python grpcio-example.py client") |
|
0 commit comments