• 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"""An example of compression on the client side with gRPC."""
15
16from __future__ import absolute_import
17from __future__ import division
18from __future__ import print_function
19
20import argparse
21import logging
22import grpc
23
24from examples import helloworld_pb2
25from examples import helloworld_pb2_grpc
26
27_DESCRIPTION = 'A client capable of compression.'
28_COMPRESSION_OPTIONS = {
29    "none": grpc.Compression.NoCompression,
30    "deflate": grpc.Compression.Deflate,
31    "gzip": grpc.Compression.Gzip,
32}
33
34_LOGGER = logging.getLogger(__name__)
35
36
37def run_client(channel_compression, call_compression, target):
38    with grpc.insecure_channel(target,
39                               compression=channel_compression) as channel:
40        stub = helloworld_pb2_grpc.GreeterStub(channel)
41        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'),
42                                 compression=call_compression,
43                                 wait_for_ready=True)
44        print("Response: {}".format(response))
45
46
47def main():
48    parser = argparse.ArgumentParser(description=_DESCRIPTION)
49    parser.add_argument('--channel_compression',
50                        default='none',
51                        nargs='?',
52                        choices=_COMPRESSION_OPTIONS.keys(),
53                        help='The compression method to use for the channel.')
54    parser.add_argument(
55        '--call_compression',
56        default='none',
57        nargs='?',
58        choices=_COMPRESSION_OPTIONS.keys(),
59        help='The compression method to use for an individual call.')
60    parser.add_argument('--server',
61                        default='localhost:50051',
62                        type=str,
63                        nargs='?',
64                        help='The host-port pair at which to reach the server.')
65    args = parser.parse_args()
66    channel_compression = _COMPRESSION_OPTIONS[args.channel_compression]
67    call_compression = _COMPRESSION_OPTIONS[args.call_compression]
68    run_client(channel_compression, call_compression, args.server)
69
70
71if __name__ == "__main__":
72    logging.basicConfig()
73    main()
74