gRPC Python Reflection

1.80.0 · active · verified Sun Apr 05

grpcio-reflection is the Python implementation of the standard Protobuf Reflection Service for gRPC. It enables gRPC clients to dynamically discover available services, their RPC methods, and associated message types at runtime, without needing pre-compiled .proto files. This is particularly useful for debugging tools like grpcurl and Postman. The library is currently at version 1.80.0 and follows the gRPC core's approximately six-week release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to enable server reflection for a gRPC Python server. It includes a mock gRPC service setup to illustrate the integration of `grpcio-reflection`. In a real application, you would replace the `MockService_pb2` and `MockService_pb2_grpc` with the actual modules generated from your `.proto` files using `grpcio-tools`. The `reflection.enable_server_reflection` function is called with a list of fully-qualified service names you wish to expose.

import grpc
from concurrent import futures
from grpc_reflection.v1alpha import reflection

# --- Simulate generated protobuf code ---
# In a real application, these would be generated by grpcio-tools
# from your .proto files (e.g., my_service.proto).
# For this example, we define minimal mock objects.
class MockService_pb2:
    DESCRIPTOR = type('Descriptor', (object,), {'services_by_name': {'MyService': type('ServiceDescriptor', (object,), {'full_name': 'my.package.MyService'})}}})()

class MockService_pb2_grpc:
    class MyServiceServicer:
        def __init__(self):
            pass

    def add_MyServiceServicer_to_server(servicer, server):
        print(f"Mock: Adding {servicer.__class__.__name__} to server")

# --- Real gRPC server with reflection ---

class MyServiceServicer(MockService_pb2_grpc.MyServiceServicer):
    def MyMethod(self, request, context):
        # In a real scenario, handle actual RPC logic
        print(f"Received request for MyMethod: {request}")
        return "Response from MyMethod"

def serve_with_reflection(port='[::]:50051'):
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    # Register your actual gRPC service implementation
    MockService_pb2_grpc.add_MyServiceServicer_to_server(MyServiceServicer(), server)

    # Enable server reflection
    # List all service names you want to expose via reflection.
    # This typically includes your custom services and the reflection service itself.
    SERVICE_NAMES = (
        MockService_pb2.DESCRIPTOR.services_by_name['MyService'].full_name,
        reflection.SERVICE_NAME,
    )
    reflection.enable_server_reflection(SERVICE_NAMES, server)

    server.add_insecure_port(port)
    server.start()
    print(f"Server with reflection enabled listening on {port}")
    server.wait_for_termination()

if __name__ == '__main__':
    # To run this, you'd typically have your actual .proto files compiled
    # and replace MockService_pb2 and MockService_pb2_grpc with your generated ones.
    # Example usage with grpcurl (after running this script):
    # grpcurl -plaintext localhost:50051 list
    # grpcurl -plaintext localhost:50051 list my.package.MyService
    # grpcurl -plaintext -d '{"name": "World"}' localhost:50051 my.package.MyService/MyMethod
    serve_with_reflection()

view raw JSON →