2007-03-07 05:45:10 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys
|
|
|
|
sys.path.append('../gen-py')
|
|
|
|
|
|
|
|
from tutorial import Calculator
|
|
|
|
from tutorial.ttypes import *
|
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
from thrift import Thrift
|
2007-03-07 05:45:10 +00:00
|
|
|
from thrift.transport import TSocket
|
|
|
|
from thrift.transport import TTransport
|
|
|
|
from thrift.protocol import TBinaryProtocol
|
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
try:
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
# Make socket
|
|
|
|
transport = TSocket.TSocket('localhost', 9090)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
# Buffering is critical. Raw sockets are very slow
|
|
|
|
transport = TTransport.TBufferedTransport(transport)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
# Wrap in a protocol
|
|
|
|
protocol = TBinaryProtocol.TBinaryProtocol(transport)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
# Create a client to use the protocol encoder
|
|
|
|
client = Calculator.Client(protocol)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
# Connect!
|
|
|
|
transport.open()
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
client.ping()
|
|
|
|
print 'ping()'
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
sum = client.add(1,1)
|
|
|
|
print '1+1=%d' % (sum)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
work = Work()
|
2008-02-06 22:18:40 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
work.op = Operation.DIVIDE
|
|
|
|
work.num1 = 1
|
|
|
|
work.num2 = 0
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
try:
|
|
|
|
quotient = client.calculate(1, work)
|
|
|
|
print 'Whoa? You know how to divide by zero?'
|
|
|
|
except InvalidOperation, io:
|
|
|
|
print 'InvalidOperation: %s' % (io.__str__())
|
2008-02-06 22:18:40 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
work.op = Operation.SUBTRACT
|
|
|
|
work.num1 = 15
|
|
|
|
work.num2 = 10
|
2008-02-06 22:18:40 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
diff = client.calculate(1, work)
|
|
|
|
print '15-10=%d' % (diff)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
log = client.getStruct(1)
|
|
|
|
print 'Check log: %s' % (log.value)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
# Close!
|
|
|
|
transport.close()
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
except Thrift.TException, tx:
|
|
|
|
print '%s' % (tx.message)
|