blob: 3abbbbd9ae812faafd7e28818ad35bb1e1cc4e43 [file] [log] [blame]
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +09001import argparse
Nobuaki Sukegawa85650612016-01-08 03:26:44 +09002import socket
3
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09004from local_thrift import thrift # noqa
Nobuaki Sukegawa85650612016-01-08 03:26:44 +09005from thrift.transport.TSocket import TSocket
6from thrift.transport.TTransport import TBufferedTransport, TFramedTransport
7from thrift.transport.THttpClient import THttpClient
8from thrift.protocol.TBinaryProtocol import TBinaryProtocol
9from thrift.protocol.TCompactProtocol import TCompactProtocol
10from thrift.protocol.TJSONProtocol import TJSONProtocol
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090011
12
13def add_common_args(p):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090014 p.add_argument('--host', default='localhost')
15 p.add_argument('--port', type=int, default=9090)
16 p.add_argument('--protocol', default='binary')
17 p.add_argument('--transport', default='buffered')
18 p.add_argument('--ssl', action='store_true')
Nobuaki Sukegawa378b7272016-01-03 17:04:50 +090019
20
21def parse_common_args(argv):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090022 p = argparse.ArgumentParser()
23 add_common_args(p)
24 return p.parse_args(argv)
Nobuaki Sukegawa85650612016-01-08 03:26:44 +090025
26
27def init_protocol(args):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090028 sock = TSocket(args.host, args.port, socket_family=socket.AF_INET)
29 sock.setTimeout(500)
30 trans = {
31 'buffered': TBufferedTransport,
32 'framed': TFramedTransport,
33 'http': THttpClient,
34 }[args.transport](sock)
35 trans.open()
36 return {
37 'binary': TBinaryProtocol,
38 'compact': TCompactProtocol,
39 'json': TJSONProtocol,
40 }[args.protocol](trans)