Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | sys.path.append('../gen-py') |
| 5 | |
| 6 | from tutorial import Calculator |
| 7 | from tutorial.ttypes import * |
| 8 | |
| 9 | from thrift.transport import TSocket |
| 10 | from thrift.transport import TTransport |
| 11 | from thrift.protocol import TBinaryProtocol |
| 12 | |
| 13 | # Make socket |
| 14 | transport = TSocket.TSocket('localhost', 9090) |
| 15 | |
| 16 | # Buffering is critical. Raw sockets are very slow |
| 17 | transport = TTransport.TBufferedTransport(transport) |
| 18 | |
| 19 | # Wrap in a protocol |
| 20 | protocol = TBinaryProtocol.TBinaryProtocol(transport) |
| 21 | |
| 22 | # Create a client to use the protocol encoder |
| 23 | client = Calculator.Client(protocol) |
| 24 | |
| 25 | # Connect! |
| 26 | transport.open() |
| 27 | |
| 28 | client.ping() |
| 29 | print 'ping()' |
| 30 | |
| 31 | sum = client.add(1,1) |
| 32 | print '1+1=%d' % (sum) |
| 33 | |
| 34 | work = Work() |
| 35 | |
| 36 | work.op = Operation.DIVIDE |
| 37 | work.num1 = 1 |
| 38 | work.num2 = 0 |
| 39 | |
| 40 | try: |
| 41 | quotient = client.calculate(1, work) |
| 42 | print 'Whoa? You know how to divide by zero?' |
| 43 | except InvalidOperation, io: |
| 44 | print 'InvalidOperation: %s' % (io.__str__()) |
| 45 | |
| 46 | work.op = Operation.SUBTRACT |
| 47 | work.num1 = 15 |
| 48 | work.num2 = 10 |
| 49 | |
| 50 | diff = client.calculate(1, work) |
| 51 | print '15-10=%d' % (diff) |
| 52 | |
| 53 | log = client.getStruct(1) |
| 54 | print 'Check log: %s' % (log.value) |
| 55 | |
| 56 | # Close! |
| 57 | transport.close() |
| 58 | |