Debian Codegraph @

examples/python/debug/send_message.py

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