This repository has been archived on 2018-06-04. You can view files and clone it, but cannot push or open issues/pull-requests.
thrift-helloworld/python/client.py

60 lines
1.4 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from helloworld import HelloWorld
from helloworld.ttypes import *
from helloworld.constants import *
from thrift import Thrift
from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
import args
import sys
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.TBinaryProtocolAccelerated(transport)
client = HelloWorld.Client(protocol)
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()