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