]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/tutorial/py/PythonClient.py
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / tutorial / py / 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 sys
23 import glob
24 sys.path.append('gen-py')
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 thrift import Thrift
31 from thrift.transport import TSocket
32 from thrift.transport import TTransport
33 from thrift.protocol import TBinaryProtocol
34
35
36 def main():
37 # Make socket
38 transport = TSocket.TSocket('localhost', 9090)
39
40 # Buffering is critical. Raw sockets are very slow
41 transport = TTransport.TBufferedTransport(transport)
42
43 # Wrap in a protocol
44 protocol = TBinaryProtocol.TBinaryProtocol(transport)
45
46 # Create a client to use the protocol encoder
47 client = Calculator.Client(protocol)
48
49 # Connect!
50 transport.open()
51
52 client.ping()
53 print('ping()')
54
55 sum_ = client.add(1, 1)
56 print('1+1=%d' % sum_)
57
58 work = Work()
59
60 work.op = Operation.DIVIDE
61 work.num1 = 1
62 work.num2 = 0
63
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:
69 print('InvalidOperation: %r' % e)
70
71 work.op = Operation.SUBTRACT
72 work.num1 = 15
73 work.num2 = 10
74
75 diff = client.calculate(1, work)
76 print('15-10=%d' % diff)
77
78 log = client.getStruct(1)
79 print('Check log: %s' % log.value)
80
81 # Close!
82 transport.close()
83
84
85 if __name__ == '__main__':
86 try:
87 main()
88 except Thrift.TException as tx:
89 print('%s' % tx.message)