blob: 7eed061ed78d260dc1439d29784c4dac758628e2 [file] [log] [blame]
Jens Geyer72a714e2025-08-26 22:12:07 +02001#
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 Sukegawa378b7272016-01-03 17:04:50 +090020import argparse
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090021import socket
22
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090023from local_thrift import thrift # noqa
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090024from thrift.transport.TSocket import TSocket
25from thrift.transport.TTransport import TBufferedTransport, TFramedTransport
26from thrift.transport.THttpClient import THttpClient
27from thrift.protocol.TBinaryProtocol import TBinaryProtocol
28from thrift.protocol.TCompactProtocol import TCompactProtocol
29from thrift.protocol.TJSONProtocol import TJSONProtocol
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090030
31
32def add_common_args(p):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090033 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 Sukegawa378b7272016-01-03 17:04:50 +090038
39
40def parse_common_args(argv):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090041 p = argparse.ArgumentParser()
42 add_common_args(p)
43 return p.parse_args(argv)
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090044
45
46def init_protocol(args):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090047 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)