1 # gRPC Python Server Reflection 2 3 This document shows how to use gRPC Server Reflection in gRPC Python. 4 Please see [C++ Server Reflection Tutorial](../server_reflection_tutorial.md) 5 for general information and more examples how to use server reflection. 6 7 ## Enable server reflection in Python servers 8 9 gRPC Python Server Reflection is an add-on library. 10 To use it, first install the [grpcio-reflection](https://pypi.org/project/grpcio-reflection/) 11 PyPI package into your project. 12 13 Note that with Python you need to manually register the service 14 descriptors with the reflection service implementation when creating a server 15 (this isn't necessary with e.g. C++ or Java) 16 ```python 17 # add the following import statement to use server reflection 18 from grpc_reflection.v1alpha import reflection 19 # ... 20 def serve(): 21 server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) 22 helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) 23 # the reflection service will be aware of "Greeter" and "ServerReflection" services. 24 SERVICE_NAMES = ( 25 helloworld_pb2.DESCRIPTOR.services_by_name['Greeter'].full_name, 26 reflection.SERVICE_NAME, 27 ) 28 reflection.enable_server_reflection(SERVICE_NAMES, server) 29 server.add_insecure_port('[::]:50051') 30 server.start() 31 ``` 32 33 Please see 34 [greeter_server_with_reflection.py](https://github.com/grpc/grpc/blob/master/examples/python/helloworld/greeter_server_with_reflection.py) 35 in the examples directory for the full example, which extends the gRPC [Python 36 `Greeter` example](https://github.com/grpc/tree/master/examples/python/helloworld) on a 37 reflection-enabled server. 38 39 After starting the server, you can verify that the server reflection 40 is working properly by using the [`grpc_cli` command line 41 tool](https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md): 42 43 ```sh 44 $ grpc_cli ls localhost:50051 45 ``` 46 47 output: 48 ```sh 49 grpc.reflection.v1alpha.ServerReflection 50 helloworld.Greeter 51 ``` 52 53 For more examples and instructions how to use the `grpc_cli` tool, 54 please refer to the [`grpc_cli` documentation](../command_line_tool.md) 55 and the [C++ Server Reflection Tutorial](../server_reflection_tutorial.md). 56 57 ## Additional Resources 58 59 The [Server Reflection Protocol](../server-reflection.md) provides detailed 60 information about how the server reflection works and describes the server reflection 61 protocol in detail. 62