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 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 9 | from thrift import Thrift |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 10 | from thrift.transport import TSocket |
| 11 | from thrift.transport import TTransport |
| 12 | from thrift.protocol import TBinaryProtocol |
| 13 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 14 | try: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 15 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 16 | # Make socket |
| 17 | transport = TSocket.TSocket('localhost', 9090) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 18 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 19 | # Buffering is critical. Raw sockets are very slow |
| 20 | transport = TTransport.TBufferedTransport(transport) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 21 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 22 | # Wrap in a protocol |
| 23 | protocol = TBinaryProtocol.TBinaryProtocol(transport) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 24 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 25 | # Create a client to use the protocol encoder |
| 26 | client = Calculator.Client(protocol) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 27 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 28 | # Connect! |
| 29 | transport.open() |
| 30 | |
| 31 | client.ping() |
| 32 | print 'ping()' |
| 33 | |
| 34 | sum = client.add(1,1) |
| 35 | print '1+1=%d' % (sum) |
| 36 | |
| 37 | work = Work() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 38 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 39 | work.op = Operation.DIVIDE |
| 40 | work.num1 = 1 |
| 41 | work.num2 = 0 |
| 42 | |
| 43 | try: |
| 44 | quotient = client.calculate(1, work) |
| 45 | print 'Whoa? You know how to divide by zero?' |
| 46 | except InvalidOperation, io: |
| 47 | print 'InvalidOperation: %s' % (io.__str__()) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 48 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 49 | work.op = Operation.SUBTRACT |
| 50 | work.num1 = 15 |
| 51 | work.num2 = 10 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 52 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 53 | diff = client.calculate(1, work) |
| 54 | print '15-10=%d' % (diff) |
| 55 | |
| 56 | log = client.getStruct(1) |
| 57 | print 'Check log: %s' % (log.value) |
| 58 | |
| 59 | # Close! |
| 60 | transport.close() |
| 61 | |
| 62 | except Thrift.TException, tx: |
| 63 | print '%s' % (tx.message) |