| sys.path.append('../gen-py') |
| from tutorial import Calculator |
| from tutorial.ttypes import * |
| from thrift.transport import TSocket |
| from thrift.transport import TTransport |
| from thrift.protocol import TBinaryProtocol |
| transport = TSocket.TSocket('localhost', 9090) |
| # Buffering is critical. Raw sockets are very slow |
| transport = TTransport.TBufferedTransport(transport) |
| protocol = TBinaryProtocol.TBinaryProtocol(transport) |
| # Create a client to use the protocol encoder |
| client = Calculator.Client(protocol) |
| work.op = Operation.DIVIDE |
| quotient = client.calculate(1, work) |
| print 'Whoa? You know how to divide by zero?' |
| except InvalidOperation, io: |
| print 'InvalidOperation: %s' % (io.__str__()) |
| work.op = Operation.SUBTRACT |
| diff = client.calculate(1, work) |
| print '15-10=%d' % (diff) |
| log = client.getStruct(1) |
| print 'Check log: %s' % (log.value) |