blob: 9e086f042c033012e8a81d4b665c6ebd48604421 [file] [log] [blame]
Roger Meiercd9bc462011-01-03 20:19:07 +00001#!/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 Meier1d66d062012-10-26 21:46:18 +000022import sys, glob
23sys.path.append('gen-py.twisted')
24sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0])
Roger Meiercd9bc462011-01-03 20:19:07 +000025
26from tutorial import Calculator
27from tutorial.ttypes import *
28
29from twisted.internet.defer import inlineCallbacks
30from twisted.internet import reactor
31from twisted.internet.protocol import ClientCreator
32
33from thrift import Thrift
34from thrift.transport import TTwisted
35from thrift.protocol import TBinaryProtocol
36
37@inlineCallbacks
38def main(client):
39 yield client.ping()
40 print 'ping()'
41
42 sum = yield client.add(1,1)
43 print '1+1=%d' % (sum)
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)
53 print 'Whoa? You know how to divide by zero?'
54 except InvalidOperation, io:
55 print 'InvalidOperation: %r' % io
56
57 work.op = Operation.SUBTRACT
58 work.num1 = 15
59 work.num2 = 10
60
61 diff = yield client.calculate(1, work)
62 print '15-10=%d' % (diff)
63
64 log = yield client.getStruct(1)
65 print 'Check log: %s' % (log.value)
66 reactor.stop()
67
68if __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()