examples/python/xds/client.py
Go to the documentation of this file.
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:
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)
helloworld_pb2_grpc.GreeterStub
Definition: helloworld/helloworld_pb2_grpc.py:7
grpc.insecure_channel
def insecure_channel(target, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1962
grpc::experimental.insecure_channel_credentials
def insecure_channel_credentials()
Definition: src/python/grpcio/grpc/experimental/__init__.py:51
helloworld_pb2.HelloRequest
HelloRequest
Definition: helloworld/helloworld_pb2.py:93
grpc::experimental
Definition: include/grpcpp/channel.h:46
grpc.xds_channel_credentials
def xds_channel_credentials(fallback_credentials=None)
Definition: src/python/grpcio/grpc/__init__.py:1629
client.run
def run()
Definition: examples/python/async_streaming/client.py:109
grpc.secure_channel
def secure_channel(target, credentials, options=None, compression=None)
Definition: src/python/grpcio/grpc/__init__.py:1982


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:46