Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 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.twisted') |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 24 | sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0]) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 25 | |
| 26 | from tutorial import Calculator |
| 27 | from tutorial.ttypes import * |
| 28 | |
| 29 | from twisted.internet.defer import inlineCallbacks |
| 30 | from twisted.internet import reactor |
| 31 | from twisted.internet.protocol import ClientCreator |
| 32 | |
| 33 | from thrift import Thrift |
| 34 | from thrift.transport import TTwisted |
| 35 | from thrift.protocol import TBinaryProtocol |
| 36 | |
| 37 | @inlineCallbacks |
| 38 | def main(client): |
| 39 | yield client.ping() |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 40 | print('ping()') |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 41 | |
| 42 | sum = yield client.add(1,1) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 43 | print(('1+1=%d' % (sum))) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 44 | |
| 45 | work = Work() |
| 46 | |
| 47 | work.op = Operation.DIVIDE |
| 48 | work.num1 = 1 |
| 49 | work.num2 = 0 |
| 50 | |
| 51 | try: |
| 52 | quotient = yield client.calculate(1, work) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 53 | print('Whoa? You know how to divide by zero?') |
| 54 | except InvalidOperation as e: |
| 55 | print(('InvalidOperation: %r' % e)) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 56 | |
| 57 | work.op = Operation.SUBTRACT |
| 58 | work.num1 = 15 |
| 59 | work.num2 = 10 |
| 60 | |
| 61 | diff = yield client.calculate(1, work) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 62 | print(('15-10=%d' % (diff))) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 63 | |
| 64 | log = yield client.getStruct(1) |
Nobuaki Sukegawa | 760511f | 2015-11-06 21:24:16 +0900 | [diff] [blame] | 65 | print(('Check log: %s' % (log.value))) |
Roger Meier | cd9bc46 | 2011-01-03 20:19:07 +0000 | [diff] [blame] | 66 | reactor.stop() |
| 67 | |
| 68 | if __name__ == '__main__': |
| 69 | d = ClientCreator(reactor, |
| 70 | TTwisted.ThriftClientProtocol, |
| 71 | Calculator.Client, |
| 72 | TBinaryProtocol.TBinaryProtocolFactory(), |
| 73 | ).connectTCP("127.0.0.1", 9090) |
| 74 | d.addCallback(lambda conn: conn.client) |
| 75 | d.addCallback(main) |
| 76 | |
| 77 | reactor.run() |