Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 4 | # Licensed to the Apache Software Foundation (ASF) under one |
| 5 | # or more contributor license agreements. See the NOTICE file |
| 6 | # distributed with this work for additional information |
| 7 | # regarding copyright ownership. The ASF licenses this file |
| 8 | # to you under the Apache License, Version 2.0 (the |
| 9 | # "License"); you may not use this file except in compliance |
| 10 | # with the License. You may obtain a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, |
| 15 | # software distributed under the License is distributed on an |
| 16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 | # KIND, either express or implied. See the License for the |
| 18 | # specific language governing permissions and limitations |
| 19 | # under the License. |
| 20 | # |
| 21 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 22 | import sys |
| 23 | sys.path.append('../gen-py') |
| 24 | |
| 25 | from tutorial import Calculator |
| 26 | from tutorial.ttypes import * |
| 27 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 28 | from thrift import Thrift |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 29 | from thrift.transport import TSocket |
| 30 | from thrift.transport import TTransport |
| 31 | from thrift.protocol import TBinaryProtocol |
| 32 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 33 | try: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 34 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 35 | # Make socket |
| 36 | transport = TSocket.TSocket('localhost', 9090) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 37 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 38 | # Buffering is critical. Raw sockets are very slow |
| 39 | transport = TTransport.TBufferedTransport(transport) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 40 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 41 | # Wrap in a protocol |
| 42 | protocol = TBinaryProtocol.TBinaryProtocol(transport) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 43 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 44 | # Create a client to use the protocol encoder |
| 45 | client = Calculator.Client(protocol) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 46 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 47 | # Connect! |
| 48 | transport.open() |
| 49 | |
| 50 | client.ping() |
| 51 | print 'ping()' |
| 52 | |
| 53 | sum = client.add(1,1) |
| 54 | print '1+1=%d' % (sum) |
| 55 | |
| 56 | work = Work() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 57 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 58 | work.op = Operation.DIVIDE |
| 59 | work.num1 = 1 |
| 60 | work.num2 = 0 |
| 61 | |
| 62 | try: |
| 63 | quotient = client.calculate(1, work) |
| 64 | print 'Whoa? You know how to divide by zero?' |
| 65 | except InvalidOperation, io: |
David Reiss | 8bb593e | 2009-01-24 19:37:41 +0000 | [diff] [blame] | 66 | print 'InvalidOperation: %r' % io |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 67 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 68 | work.op = Operation.SUBTRACT |
| 69 | work.num1 = 15 |
| 70 | work.num2 = 10 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 71 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 72 | diff = client.calculate(1, work) |
| 73 | print '15-10=%d' % (diff) |
| 74 | |
| 75 | log = client.getStruct(1) |
| 76 | print 'Check log: %s' % (log.value) |
| 77 | |
| 78 | # Close! |
| 79 | transport.close() |
| 80 | |
| 81 | except Thrift.TException, tx: |
| 82 | print '%s' % (tx.message) |