• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019 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"""Send multiple greeting messages to the backend."""
15
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import logging
21import argparse
22import grpc
23from examples import helloworld_pb2
24from examples import helloworld_pb2_grpc
25
26
27def process(stub, request):
28    try:
29        response = stub.SayHello(request)
30    except grpc.RpcError as rpc_error:
31        print('Received error: %s' % rpc_error)
32    else:
33        print('Received message: %s' % response)
34
35
36def run(addr, n):
37    with grpc.insecure_channel(addr) as channel:
38        stub = helloworld_pb2_grpc.GreeterStub(channel)
39        request = helloworld_pb2.HelloRequest(name='you')
40        for _ in range(n):
41            process(stub, request)
42
43
44def main():
45    parser = argparse.ArgumentParser()
46    parser.add_argument('--addr',
47                        nargs=1,
48                        type=str,
49                        default='[::]:50051',
50                        help='the address to request')
51    parser.add_argument('-n',
52                        nargs=1,
53                        type=int,
54                        default=10,
55                        help='an integer for number of messages to sent')
56    args = parser.parse_args()
57    run(addr=args.addr, n=args.n)
58
59
60if __name__ == '__main__':
61    logging.basicConfig()
62    main()
63