1# gRPC Python Server Reflection
2
3This document shows how to use gRPC Server Reflection in gRPC Python.
4Please see [C++ Server Reflection Tutorial](../server_reflection_tutorial.md)
5for general information and 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.
10To use it, first install the [grpcio-reflection](https://pypi.org/project/grpcio-reflection/)
11PyPI package into your project.
12
13Note that with Python you need to manually register the service
14descriptors 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
18from grpc_reflection.v1alpha import reflection
19# ...
20def 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
33Please see
34[greeter_server_with_reflection.py](https://github.com/grpc/grpc/blob/master/examples/python/helloworld/greeter_server_with_reflection.py)
35in 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
37reflection-enabled server.
38
39After starting the server, you can verify that the server reflection
40is working properly by using the [`grpc_cli` command line
41tool](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
59The [Server Reflection Protocol](../server-reflection.md) provides detailed
60information about how the server reflection works and describes the server reflection
61protocol in detail.
62