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 | |
Roger Meier | 1d66d06 | 2012-10-26 21:46:18 +0000 | [diff] [blame] | 22 | import sys, glob |
| 23 | sys.path.append('gen-py') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 24 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 25 | |
| 26 | from tutorial import Calculator |
| 27 | from tutorial.ttypes import * |
| 28 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 29 | from thrift import Thrift |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 30 | from thrift.transport import TSocket |
| 31 | from thrift.transport import TTransport |
| 32 | from thrift.protocol import TBinaryProtocol |
| 33 | |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 34 | try: |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 35 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 36 | # Make socket |
| 37 | transport = TSocket.TSocket('localhost', 9090) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 38 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 39 | # Buffering is critical. Raw sockets are very slow |
| 40 | transport = TTransport.TBufferedTransport(transport) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 41 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 42 | # Wrap in a protocol |
| 43 | protocol = TBinaryProtocol.TBinaryProtocol(transport) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 44 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 45 | # Create a client to use the protocol encoder |
| 46 | client = Calculator.Client(protocol) |
Mark Slee | 07a3aab | 2007-03-07 05:45:10 +0000 | [diff] [blame] | 47 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 48 | # Connect! |
| 49 | transport.open() |
| 50 | |
| 51 | client.ping() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 52 | print('ping()') |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 53 | |
| 54 | sum = client.add(1,1) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 55 | print(('1+1=%d' % (sum))) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 56 | |
| 57 | work = Work() |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 58 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 59 | work.op = Operation.DIVIDE |
| 60 | work.num1 = 1 |
| 61 | work.num2 = 0 |
| 62 | |
| 63 | try: |
| 64 | quotient = client.calculate(1, work) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 65 | print('Whoa? You know how to divide by zero?') |
| 66 | except InvalidOperation as e: |
| 67 | print(('InvalidOperation: %r' % e)) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 68 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 69 | work.op = Operation.SUBTRACT |
| 70 | work.num1 = 15 |
| 71 | work.num2 = 10 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 72 | |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 73 | diff = client.calculate(1, work) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 74 | print(('15-10=%d' % (diff))) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 75 | |
| 76 | log = client.getStruct(1) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 77 | print(('Check log: %s' % (log.value))) |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 78 | |
| 79 | # Close! |
| 80 | transport.close() |
| 81 | |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame^] | 82 | except Thrift.TException as tx: |
| 83 | print(('%s' % (tx.message))) |