added a proper header, added command line args (using args package), switched to use TBinaryProtocolAccelerated for the protocol

This commit is contained in:
Ray Slakinski 2012-05-18 14:45:17 -04:00
parent 769f960401
commit 7bb53b8443

60
client.py Normal file → Executable file
View File

@ -1,3 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from helloworld import HelloWorld from helloworld import HelloWorld
from helloworld.ttypes import * from helloworld.ttypes import *
from helloworld.constants import * from helloworld.constants import *
@ -6,22 +9,51 @@ from thrift import Thrift
from thrift.transport import TSocket, TTransport from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol from thrift.protocol import TBinaryProtocol
import args
import sys
try: def usage():
transport = TSocket.TSocket('localhost', 9090) print """
transport = TTransport.TBufferedTransport(transport) Usage: client.py --ping OR --name <your name> [options] [args]
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = HelloWorld.Client(protocol)
transport.open() Required:
--name=Your Name
--ping
Options:
--lang <en,fr,jp,kr,kp> Used with --name. Changes the greeting message for the given language
"""
resp = client.ping()
print resp
msg = Message(name='Ray', lang='jp') def main():
resp = client.sayHello(msg) try:
print resp transport = TSocket.TSocket('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
client = HelloWorld.Client(protocol)
transport.close() transport.open()
except Thrift.TException, tx:
print tx.message 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()