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