xref: /aosp_15_r20/external/grpc-grpc/examples/python/debug/asyncio_get_stats.py (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1# Copyright 2020 The gRPC Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Poll statistics from the server."""
15
16import argparse
17import asyncio
18import logging
19
20import grpc
21from grpc_channelz.v1 import channelz_pb2
22from grpc_channelz.v1 import channelz_pb2_grpc
23
24
25async def run(addr: str) -> None:
26    async with grpc.aio.insecure_channel(addr) as channel:
27        channelz_stub = channelz_pb2_grpc.ChannelzStub(channel)
28        response = await channelz_stub.GetServers(
29            channelz_pb2.GetServersRequest(start_server_id=0)
30        )
31        print("Info for all servers: %s" % response)
32
33
34async def main() -> None:
35    parser = argparse.ArgumentParser()
36    parser.add_argument(
37        "--addr",
38        nargs=1,
39        type=str,
40        default="[::]:50051",
41        help="the address to request",
42    )
43    args = parser.parse_args()
44    run(addr=args.addr)
45
46
47if __name__ == "__main__":
48    logging.basicConfig()
49    asyncio.get_event_loop().run_until_complete(main())
50