Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 1 | import argparse |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 2 | import socket |
| 3 | |
Nobuaki Sukegawa | d479e23 | 2016-02-28 11:28:19 +0900 | [diff] [blame] | 4 | from local_thrift import thrift # noqa |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 5 | from thrift.transport.TSocket import TSocket |
| 6 | from thrift.transport.TTransport import TBufferedTransport, TFramedTransport |
| 7 | from thrift.transport.THttpClient import THttpClient |
| 8 | from thrift.protocol.TBinaryProtocol import TBinaryProtocol |
| 9 | from thrift.protocol.TCompactProtocol import TCompactProtocol |
| 10 | from thrift.protocol.TJSONProtocol import TJSONProtocol |
Nobuaki Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def add_common_args(p): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 14 | 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 Sukegawa | 378b727 | 2016-01-03 17:04:50 +0900 | [diff] [blame] | 19 | |
| 20 | |
| 21 | def parse_common_args(argv): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 22 | p = argparse.ArgumentParser() |
| 23 | add_common_args(p) |
| 24 | return p.parse_args(argv) |
Nobuaki Sukegawa | 8565061 | 2016-01-08 03:26:44 +0900 | [diff] [blame] | 25 | |
| 26 | |
| 27 | def init_protocol(args): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 28 | 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) |