Debian Codegraph @

examples/python/xds/client.py

raw
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
16 from __future__ import print_function
17
18 import argparse
19 import logging
20
21 import grpc
22 import grpc.experimental
23 import helloworld_pb2
24 import helloworld_pb2_grpc
25
26 _DESCRIPTION = "Get a greeting from a server."
27
28
29 def 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
42 if __name__ == '__main__':
43 parser = argparse.ArgumentParser(description=_DESCRIPTION)
44 parser.add_argument("server",
45 default=None,
46 help="The address of the server.")
47 parser.add_argument(
48 "--xds-creds",
49 action="store_true",
50 help="If specified, uses xDS credentials to connect to the server.")
51 args = parser.parse_args()
52 logging.basicConfig()
53 run(args.server, args.xds_creds)