]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/tutorial/py.twisted/PythonClient.py
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / tutorial / py.twisted / PythonClient.py
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
22 import glob
23 import sys
24 sys.path.append('gen-py.twisted')
25 sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
26
27 from tutorial import Calculator
28 from tutorial.ttypes import InvalidOperation, Operation, Work
29
30 from twisted.internet.defer import inlineCallbacks
31 from twisted.internet import reactor
32 from twisted.internet.protocol import ClientCreator
33
34 from thrift.transport import TTwisted
35 from thrift.protocol import TBinaryProtocol
36
37
38 @inlineCallbacks
39 def main(client):
40 yield client.ping()
41 print('ping()')
42
43 sum = yield client.add(1, 1)
44 print(('1+1=%d' % (sum)))
45
46 work = Work()
47
48 work.op = Operation.DIVIDE
49 work.num1 = 1
50 work.num2 = 0
51
52 try:
53 quotient = yield client.calculate(1, work)
54 print('Whoa? You know how to divide by zero?')
55 print('FYI the answer is %d' % quotient)
56 except InvalidOperation as e:
57 print(('InvalidOperation: %r' % e))
58
59 work.op = Operation.SUBTRACT
60 work.num1 = 15
61 work.num2 = 10
62
63 diff = yield client.calculate(1, work)
64 print(('15-10=%d' % (diff)))
65
66 log = yield client.getStruct(1)
67 print(('Check log: %s' % (log.value)))
68 reactor.stop()
69
70
71 if __name__ == '__main__':
72 d = ClientCreator(reactor,
73 TTwisted.ThriftClientProtocol,
74 Calculator.Client,
75 TBinaryProtocol.TBinaryProtocolFactory(),
76 ).connectTCP("127.0.0.1", 9090)
77 d.addCallback(lambda conn: conn.client)
78 d.addCallback(main)
79
80 reactor.run()