diff --git a/client.py b/client.py old mode 100644 new mode 100755 index afe1443..9507cfa --- a/client.py +++ b/client.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + from helloworld import HelloWorld from helloworld.ttypes import * from helloworld.constants import * @@ -6,22 +9,51 @@ from thrift import Thrift from thrift.transport import TSocket, TTransport from thrift.protocol import TBinaryProtocol +import args +import sys -try: - transport = TSocket.TSocket('localhost', 9090) - transport = TTransport.TBufferedTransport(transport) - protocol = TBinaryProtocol.TBinaryProtocol(transport) - client = HelloWorld.Client(protocol) +def usage(): + print """ +Usage: client.py --ping OR --name [options] [args] - transport.open() +Required: + --name=Your Name + --ping +Options: + --lang Used with --name. Changes the greeting message for the given language +""" - resp = client.ping() - print resp - msg = Message(name='Ray', lang='jp') - resp = client.sayHello(msg) - print resp +def main(): + try: + transport = TSocket.TSocket('localhost', 9090) + transport = TTransport.TBufferedTransport(transport) + protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport) + client = HelloWorld.Client(protocol) - transport.close() -except Thrift.TException, tx: - print tx.message + transport.open() + + if '--ping' in args.grouped: + resp = client.ping() + print resp + sys.exit() + + if not '--name' in args.grouped: + usage() + sys.exit(1) + name = args.grouped['--name'][0] + + lang = 'en' + if args.grouped.get('--lang', None): + lang = args.grouped['--lang'][0].lower() + + msg = Message(name, lang) + resp = client.sayHello(msg) + print resp + + transport.close() + except Thrift.TException, tx: + print tx.message + +if __name__ == '__main__': + main()