{"library":"grpcio-reflection","code":"import grpc\nfrom concurrent import futures\nfrom grpc_reflection.v1alpha import reflection\n\n# --- Simulate generated protobuf code ---\n# In a real application, these would be generated by grpcio-tools\n# from your .proto files (e.g., my_service.proto).\n# For this example, we define minimal mock objects.\nclass MockService_pb2:\n    DESCRIPTOR = type('Descriptor', (object,), {'services_by_name': {'MyService': type('ServiceDescriptor', (object,), {'full_name': 'my.package.MyService'})}}})()\n\nclass MockService_pb2_grpc:\n    class MyServiceServicer:\n        def __init__(self):\n            pass\n\n    def add_MyServiceServicer_to_server(servicer, server):\n        print(f\"Mock: Adding {servicer.__class__.__name__} to server\")\n\n# --- Real gRPC server with reflection ---\n\nclass MyServiceServicer(MockService_pb2_grpc.MyServiceServicer):\n    def MyMethod(self, request, context):\n        # In a real scenario, handle actual RPC logic\n        print(f\"Received request for MyMethod: {request}\")\n        return \"Response from MyMethod\"\n\ndef serve_with_reflection(port='[::]:50051'):\n    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))\n\n    # Register your actual gRPC service implementation\n    MockService_pb2_grpc.add_MyServiceServicer_to_server(MyServiceServicer(), server)\n\n    # Enable server reflection\n    # List all service names you want to expose via reflection.\n    # This typically includes your custom services and the reflection service itself.\n    SERVICE_NAMES = (\n        MockService_pb2.DESCRIPTOR.services_by_name['MyService'].full_name,\n        reflection.SERVICE_NAME,\n    )\n    reflection.enable_server_reflection(SERVICE_NAMES, server)\n\n    server.add_insecure_port(port)\n    server.start()\n    print(f\"Server with reflection enabled listening on {port}\")\n    server.wait_for_termination()\n\nif __name__ == '__main__':\n    # To run this, you'd typically have your actual .proto files compiled\n    # and replace MockService_pb2 and MockService_pb2_grpc with your generated ones.\n    # Example usage with grpcurl (after running this script):\n    # grpcurl -plaintext localhost:50051 list\n    # grpcurl -plaintext localhost:50051 list my.package.MyService\n    # grpcurl -plaintext -d '{\"name\": \"World\"}' localhost:50051 my.package.MyService/MyMethod\n    serve_with_reflection()","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}