xref: /aosp_15_r20/external/grpc-grpc/doc/python/server_reflection.md (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1*cc02d7e2SAndroid Build Coastguard Worker# gRPC Python Server Reflection
2*cc02d7e2SAndroid Build Coastguard Worker
3*cc02d7e2SAndroid Build Coastguard WorkerThis document shows how to use gRPC Server Reflection in gRPC Python.
4*cc02d7e2SAndroid Build Coastguard WorkerPlease see [C++ Server Reflection Tutorial] for general information
5*cc02d7e2SAndroid Build Coastguard Workerand more examples how to use server reflection.
6*cc02d7e2SAndroid Build Coastguard Worker
7*cc02d7e2SAndroid Build Coastguard Worker## Enable server reflection in Python servers
8*cc02d7e2SAndroid Build Coastguard Worker
9*cc02d7e2SAndroid Build Coastguard WorkergRPC Python Server Reflection is an add-on library. To use it, first install
10*cc02d7e2SAndroid Build Coastguard Workerthe [grpcio-reflection] PyPI package into your project.
11*cc02d7e2SAndroid Build Coastguard Worker
12*cc02d7e2SAndroid Build Coastguard WorkerNote that with Python you need to manually register the service
13*cc02d7e2SAndroid Build Coastguard Workerdescriptors with the reflection service implementation when creating a server
14*cc02d7e2SAndroid Build Coastguard Worker(this isn't necessary with e.g. C++ or Java)
15*cc02d7e2SAndroid Build Coastguard Worker```python
16*cc02d7e2SAndroid Build Coastguard Worker# add the following import statement to use server reflection
17*cc02d7e2SAndroid Build Coastguard Workerfrom grpc_reflection.v1alpha import reflection
18*cc02d7e2SAndroid Build Coastguard Worker# ...
19*cc02d7e2SAndroid Build Coastguard Workerdef serve():
20*cc02d7e2SAndroid Build Coastguard Worker    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
21*cc02d7e2SAndroid Build Coastguard Worker    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
22*cc02d7e2SAndroid Build Coastguard Worker    # the reflection service will be aware of "Greeter" and "ServerReflection" services.
23*cc02d7e2SAndroid Build Coastguard Worker    SERVICE_NAMES = (
24*cc02d7e2SAndroid Build Coastguard Worker        helloworld_pb2.DESCRIPTOR.services_by_name['Greeter'].full_name,
25*cc02d7e2SAndroid Build Coastguard Worker        reflection.SERVICE_NAME,
26*cc02d7e2SAndroid Build Coastguard Worker    )
27*cc02d7e2SAndroid Build Coastguard Worker    reflection.enable_server_reflection(SERVICE_NAMES, server)
28*cc02d7e2SAndroid Build Coastguard Worker    server.add_insecure_port('[::]:50051')
29*cc02d7e2SAndroid Build Coastguard Worker    server.start()
30*cc02d7e2SAndroid Build Coastguard Worker```
31*cc02d7e2SAndroid Build Coastguard Worker
32*cc02d7e2SAndroid Build Coastguard WorkerPlease see [greeter_server_with_reflection.py] in the examples directory for the full
33*cc02d7e2SAndroid Build Coastguard Workerexample, which extends the gRPC [Python `Greeter` example] on a reflection-enabled server.
34*cc02d7e2SAndroid Build Coastguard Worker
35*cc02d7e2SAndroid Build Coastguard WorkerAfter starting the server, you can verify that the server reflection
36*cc02d7e2SAndroid Build Coastguard Workeris working properly by using the [`grpc_cli` command line tool]:
37*cc02d7e2SAndroid Build Coastguard Worker
38*cc02d7e2SAndroid Build Coastguard Worker ```sh
39*cc02d7e2SAndroid Build Coastguard Worker  $ grpc_cli ls localhost:50051
40*cc02d7e2SAndroid Build Coastguard Worker  ```
41*cc02d7e2SAndroid Build Coastguard Worker
42*cc02d7e2SAndroid Build Coastguard Worker  output:
43*cc02d7e2SAndroid Build Coastguard Worker  ```sh
44*cc02d7e2SAndroid Build Coastguard Worker  grpc.reflection.v1alpha.ServerReflection
45*cc02d7e2SAndroid Build Coastguard Worker  helloworld.Greeter
46*cc02d7e2SAndroid Build Coastguard Worker  ```
47*cc02d7e2SAndroid Build Coastguard Worker
48*cc02d7e2SAndroid Build Coastguard Worker  For more examples and instructions how to use the `grpc_cli` tool,
49*cc02d7e2SAndroid Build Coastguard Worker  please refer to the [`grpc_cli` documentation] and the
50*cc02d7e2SAndroid Build Coastguard Worker  [C++ Server Reflection Tutorial].
51*cc02d7e2SAndroid Build Coastguard Worker
52*cc02d7e2SAndroid Build Coastguard Worker
53*cc02d7e2SAndroid Build Coastguard Worker## Use Server Reflection in a Python client
54*cc02d7e2SAndroid Build Coastguard Worker
55*cc02d7e2SAndroid Build Coastguard WorkerServer Reflection can be used by clients to get information about gRPC services
56*cc02d7e2SAndroid Build Coastguard Workerat runtime. We've provided a descriptor database called
57*cc02d7e2SAndroid Build Coastguard Worker[ProtoReflectionDescriptorDatabase](../../src/python/grpcio_reflection/grpc_reflection/v1alpha/proto_reflection_descriptor_database.py)
58*cc02d7e2SAndroid Build Coastguard Workerwhich implements the
59*cc02d7e2SAndroid Build Coastguard Worker[DescriptorDatabase](https://googleapis.dev/python/protobuf/latest/google/protobuf/descriptor_database.html#google.protobuf.descriptor_database.DescriptorDatabase)
60*cc02d7e2SAndroid Build Coastguard Workerinterface. It manages the communication between clients and reflection services
61*cc02d7e2SAndroid Build Coastguard Workerand the storage of received information. Clients can use it as using a local
62*cc02d7e2SAndroid Build Coastguard Workerdescriptor database.
63*cc02d7e2SAndroid Build Coastguard Worker
64*cc02d7e2SAndroid Build Coastguard Worker- To use Server Reflection with ProtoReflectionDescriptorDatabase, first
65*cc02d7e2SAndroid Build Coastguard Worker  initialize an instance with a channel.
66*cc02d7e2SAndroid Build Coastguard Worker
67*cc02d7e2SAndroid Build Coastguard Worker  ```Python
68*cc02d7e2SAndroid Build Coastguard Worker  import grpc
69*cc02d7e2SAndroid Build Coastguard Worker  from grpc_reflection.v1alpha.proto_reflection_descriptor_database import ProtoReflectionDescriptorDatabase
70*cc02d7e2SAndroid Build Coastguard Worker
71*cc02d7e2SAndroid Build Coastguard Worker  channel = grpc.secure_channel(server_address, creds)
72*cc02d7e2SAndroid Build Coastguard Worker  reflection_db = ProtoReflectionDescriptorDatabase(channel)
73*cc02d7e2SAndroid Build Coastguard Worker  ```
74*cc02d7e2SAndroid Build Coastguard Worker
75*cc02d7e2SAndroid Build Coastguard Worker- Then use this instance to feed a
76*cc02d7e2SAndroid Build Coastguard Worker  [DescriptorPool](https://googleapis.dev/python/protobuf/latest/google/protobuf/descriptor_pool.html#google.protobuf.descriptor_pool.DescriptorPool).
77*cc02d7e2SAndroid Build Coastguard Worker
78*cc02d7e2SAndroid Build Coastguard Worker  ```Python
79*cc02d7e2SAndroid Build Coastguard Worker  from google.protobuf.descriptor_pool import DescriptorPool
80*cc02d7e2SAndroid Build Coastguard Worker
81*cc02d7e2SAndroid Build Coastguard Worker  desc_pool = DescriptorPool(reflection_db)
82*cc02d7e2SAndroid Build Coastguard Worker  ```
83*cc02d7e2SAndroid Build Coastguard Worker
84*cc02d7e2SAndroid Build Coastguard Worker- Example usage of this descriptor pool:
85*cc02d7e2SAndroid Build Coastguard Worker
86*cc02d7e2SAndroid Build Coastguard Worker  * Get Service/method descriptors.
87*cc02d7e2SAndroid Build Coastguard Worker
88*cc02d7e2SAndroid Build Coastguard Worker    ```Python
89*cc02d7e2SAndroid Build Coastguard Worker    service_desc = desc_pool.FindServiceByName("helloworld.Greeter")
90*cc02d7e2SAndroid Build Coastguard Worker    method_desc = service_desc.FindMethodByName("helloworld.Greeter.SayHello")
91*cc02d7e2SAndroid Build Coastguard Worker    ```
92*cc02d7e2SAndroid Build Coastguard Worker
93*cc02d7e2SAndroid Build Coastguard Worker  * Get message type descriptors and create messages dynamically.
94*cc02d7e2SAndroid Build Coastguard Worker
95*cc02d7e2SAndroid Build Coastguard Worker    ```Python
96*cc02d7e2SAndroid Build Coastguard Worker    request_desc = desc_pool.FindMessageTypeByName("helloworld.HelloRequest")
97*cc02d7e2SAndroid Build Coastguard Worker    request = MessageFactory(desc_pool).GetPrototype(request_desc)()
98*cc02d7e2SAndroid Build Coastguard Worker    ```
99*cc02d7e2SAndroid Build Coastguard Worker
100*cc02d7e2SAndroid Build Coastguard Worker- You can also use the Reflection Database to list all the services:
101*cc02d7e2SAndroid Build Coastguard Worker
102*cc02d7e2SAndroid Build Coastguard Worker    ```Python
103*cc02d7e2SAndroid Build Coastguard Worker    services = reflection_db.get_services()
104*cc02d7e2SAndroid Build Coastguard Worker    ```
105*cc02d7e2SAndroid Build Coastguard Worker
106*cc02d7e2SAndroid Build Coastguard Worker
107*cc02d7e2SAndroid Build Coastguard Worker## Additional Resources
108*cc02d7e2SAndroid Build Coastguard Worker
109*cc02d7e2SAndroid Build Coastguard WorkerThe [Server Reflection Protocol] provides detailed
110*cc02d7e2SAndroid Build Coastguard Workerinformation about how the server reflection works and describes the server reflection
111*cc02d7e2SAndroid Build Coastguard Workerprotocol in detail.
112*cc02d7e2SAndroid Build Coastguard Worker
113*cc02d7e2SAndroid Build Coastguard Worker
114*cc02d7e2SAndroid Build Coastguard Worker[C++ Server Reflection Tutorial]: ../server_reflection_tutorial.md
115*cc02d7e2SAndroid Build Coastguard Worker[grpcio-reflection]: https://pypi.org/project/grpcio-reflection/
116*cc02d7e2SAndroid Build Coastguard Worker[greeter_server_with_reflection.py]: https://github.com/grpc/grpc/blob/master/examples/python/helloworld/greeter_server_with_reflection.py
117*cc02d7e2SAndroid Build Coastguard Worker[Python `Greeter` example]: https://github.com/grpc/grpc/tree/master/examples/python/helloworld
118*cc02d7e2SAndroid Build Coastguard Worker[`grpc_cli` command line tool]: https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md
119*cc02d7e2SAndroid Build Coastguard Worker[`grpc_cli` documentation]: ../command_line_tool.md
120*cc02d7e2SAndroid Build Coastguard Worker[C++ Server Reflection Tutorial]: ../server_reflection_tutorial.md
121*cc02d7e2SAndroid Build Coastguard Worker[Server Reflection Protocol]: ../server-reflection.md
122