xref: /aosp_15_r20/external/grpc-grpc/examples/python/xds/client.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"""The Python implementation of the GRPC helloworld.Greeter client."""
15
16from __future__ import print_function
17
18import argparse
19import logging
20
21import grpc
22import grpc.experimental
23import helloworld_pb2
24import helloworld_pb2_grpc
25
26_DESCRIPTION = "Get a greeting from a server."
27
28
29def run(server_address, secure):
30    if secure:
31        fallback_creds = grpc.experimental.insecure_channel_credentials()
32        channel_creds = grpc.xds_channel_credentials(fallback_creds)
33        channel = grpc.secure_channel(server_address, channel_creds)
34    else:
35        channel = grpc.insecure_channel(server_address)
36    with channel:
37        stub = helloworld_pb2_grpc.GreeterStub(channel)
38        response = stub.SayHello(helloworld_pb2.HelloRequest(name="you"))
39        print("Greeter client received: " + response.message)
40
41
42if __name__ == "__main__":
43    parser = argparse.ArgumentParser(description=_DESCRIPTION)
44    parser.add_argument(
45        "server", default=None, help="The address of the server."
46    )
47    parser.add_argument(
48        "--xds-creds",
49        action="store_true",
50        help="If specified, uses xDS credentials to connect to the server.",
51    )
52    args = parser.parse_args()
53    logging.basicConfig()
54    run(args.server, args.xds_creds)
55