added a proper header, added command line args (using args package), switched to use TBinaryProtocolAccelerated for the protocol
This commit is contained in:
parent
769f960401
commit
7bb53b8443
40
client.py
Normal file → Executable file
40
client.py
Normal file → Executable 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:
|
||||
def usage():
|
||||
print """
|
||||
Usage: client.py --ping OR --name <your name> [options] [args]
|
||||
|
||||
Required:
|
||||
--name=Your Name
|
||||
--ping
|
||||
Options:
|
||||
--lang <en,fr,jp,kr,kp> Used with --name. Changes the greeting message for the given language
|
||||
"""
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
transport = TSocket.TSocket('localhost', 9090)
|
||||
transport = TTransport.TBufferedTransport(transport)
|
||||
protocol = TBinaryProtocol.TBinaryProtocol(transport)
|
||||
protocol = TBinaryProtocol.TBinaryProtocolAccelerated(transport)
|
||||
client = HelloWorld.Client(protocol)
|
||||
|
||||
transport.open()
|
||||
|
||||
if '--ping' in args.grouped:
|
||||
resp = client.ping()
|
||||
print resp
|
||||
sys.exit()
|
||||
|
||||
msg = Message(name='Ray', lang='jp')
|
||||
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:
|
||||
except Thrift.TException, tx:
|
||||
print tx.message
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Reference in New Issue
Block a user