2016-01-03 08:04:50 +00:00
|
|
|
import argparse
|
2016-01-07 18:26:44 +00:00
|
|
|
import socket
|
|
|
|
|
|
|
|
from local_thrift import thrift
|
|
|
|
from thrift.transport.TSocket import TSocket
|
|
|
|
from thrift.transport.TTransport import TBufferedTransport, TFramedTransport
|
|
|
|
from thrift.transport.THttpClient import THttpClient
|
|
|
|
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
|
|
|
|
from thrift.protocol.TCompactProtocol import TCompactProtocol
|
|
|
|
from thrift.protocol.TJSONProtocol import TJSONProtocol
|
2016-01-03 08:04:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_common_args(p):
|
|
|
|
p.add_argument('--host', default='localhost')
|
2016-01-07 18:26:44 +00:00
|
|
|
p.add_argument('--port', type=int, default=9090)
|
|
|
|
p.add_argument('--protocol', default='binary')
|
|
|
|
p.add_argument('--transport', default='buffered')
|
2016-01-03 08:04:50 +00:00
|
|
|
p.add_argument('--ssl', action='store_true')
|
|
|
|
|
|
|
|
|
|
|
|
def parse_common_args(argv):
|
|
|
|
p = argparse.ArgumentParser()
|
|
|
|
add_common_args(p)
|
|
|
|
return p.parse_args(argv)
|
2016-01-07 18:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_protocol(args):
|
|
|
|
sock = TSocket(args.host, args.port, socket_family=socket.AF_INET)
|
|
|
|
sock.setTimeout(500)
|
|
|
|
trans = {
|
|
|
|
'buffered': TBufferedTransport,
|
|
|
|
'framed': TFramedTransport,
|
|
|
|
'http': THttpClient,
|
|
|
|
}[args.transport](sock)
|
|
|
|
trans.open()
|
|
|
|
return {
|
|
|
|
'binary': TBinaryProtocol,
|
|
|
|
'compact': TCompactProtocol,
|
|
|
|
'json': TJSONProtocol,
|
|
|
|
}[args.protocol](trans)
|