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

master
Ray Slakinski 2012-05-18 14:45:17 -04:00
parent 769f960401
commit 7bb53b8443
1 changed files with 46 additions and 14 deletions

60
client.py 100644 → 100755
View File

@ -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 <your name> [options] [args]
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')
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()