Jens Geyer | 72a714e | 2025-08-26 22:12:07 +0200 | [diff] [blame^] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 20 | import argparse |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 21 | import socket |
| 22 | |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 23 | from local_thrift import thrift # noqa |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 24 | from thrift.transport.TSocket import TSocket |
| 25 | from thrift.transport.TTransport import TBufferedTransport, TFramedTransport |
| 26 | from thrift.transport.THttpClient import THttpClient |
| 27 | from thrift.protocol.TBinaryProtocol import TBinaryProtocol |
| 28 | from thrift.protocol.TCompactProtocol import TCompactProtocol |
| 29 | from thrift.protocol.TJSONProtocol import TJSONProtocol |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def add_common_args(p): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 33 | p.add_argument('--host', default='localhost') |
| 34 | p.add_argument('--port', type=int, default=9090) |
| 35 | p.add_argument('--protocol', default='binary') |
| 36 | p.add_argument('--transport', default='buffered') |
| 37 | p.add_argument('--ssl', action='store_true') |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def parse_common_args(argv): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 41 | p = argparse.ArgumentParser() |
| 42 | add_common_args(p) |
| 43 | return p.parse_args(argv) |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 44 | |
| 45 | |
| 46 | def init_protocol(args): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 47 | sock = TSocket(args.host, args.port, socket_family=socket.AF_INET) |
| 48 | sock.setTimeout(500) |
| 49 | trans = { |
| 50 | 'buffered': TBufferedTransport, |
| 51 | 'framed': TFramedTransport, |
| 52 | 'http': THttpClient, |
| 53 | }[args.transport](sock) |
| 54 | trans.open() |
| 55 | return { |
| 56 | 'binary': TBinaryProtocol, |
| 57 | 'compact': TCompactProtocol, |
| 58 | 'json': TJSONProtocol, |
| 59 | }[args.protocol](trans) |