blob: a6c19664197132739aa70017598ca95535f65253 [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env python
2
David Reissea2cba82009-03-30 21:35:00 +00003#
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 Sukegawa7e286b02016-01-11 11:25:09 +090022import sys
23import glob
Roger Meier1d66d062012-10-26 21:46:18 +000024sys.path.append('gen-py')
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090025sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
Mark Slee07a3aab2007-03-07 05:45:10 +000026
27from tutorial import Calculator
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090028from tutorial.ttypes import InvalidOperation, Operation, Work
Mark Slee07a3aab2007-03-07 05:45:10 +000029
Mark Slee76791962007-03-14 02:47:35 +000030from thrift import Thrift
Mark Slee07a3aab2007-03-07 05:45:10 +000031from thrift.transport import TSocket
32from thrift.transport import TTransport
33from thrift.protocol import TBinaryProtocol
34
Mark Slee07a3aab2007-03-07 05:45:10 +000035
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090036def main():
37 # Make socket
38 transport = TSocket.TSocket('localhost', 9090)
Mark Slee07a3aab2007-03-07 05:45:10 +000039
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090040 # Buffering is critical. Raw sockets are very slow
41 transport = TTransport.TBufferedTransport(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000042
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090043 # Wrap in a protocol
44 protocol = TBinaryProtocol.TBinaryProtocol(transport)
Mark Slee07a3aab2007-03-07 05:45:10 +000045
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090046 # Create a client to use the protocol encoder
47 client = Calculator.Client(protocol)
Mark Slee07a3aab2007-03-07 05:45:10 +000048
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090049 # Connect!
50 transport.open()
Mark Slee76791962007-03-14 02:47:35 +000051
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090052 client.ping()
53 print('ping()')
Mark Slee76791962007-03-14 02:47:35 +000054
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090055 sum_ = client.add(1, 1)
56 print('1+1=%d' % sum_)
Mark Slee76791962007-03-14 02:47:35 +000057
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090058 work = Work()
David Reiss0c90f6f2008-02-06 22:18:40 +000059
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090060 work.op = Operation.DIVIDE
61 work.num1 = 1
62 work.num2 = 0
Mark Slee76791962007-03-14 02:47:35 +000063
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090064 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 Sukegawad479e232016-02-28 11:28:19 +090069 print('InvalidOperation: %r' % e)
David Reiss0c90f6f2008-02-06 22:18:40 +000070
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090071 work.op = Operation.SUBTRACT
72 work.num1 = 15
73 work.num2 = 10
David Reiss0c90f6f2008-02-06 22:18:40 +000074
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090075 diff = client.calculate(1, work)
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090076 print('15-10=%d' % diff)
Mark Slee76791962007-03-14 02:47:35 +000077
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090078 log = client.getStruct(1)
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090079 print('Check log: %s' % log.value)
Mark Slee76791962007-03-14 02:47:35 +000080
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090081 # Close!
82 transport.close()
Mark Slee76791962007-03-14 02:47:35 +000083
James E. King, III0ad20bd2017-09-30 15:44:16 -070084
Nobuaki Sukegawa7e286b02016-01-11 11:25:09 +090085if __name__ == '__main__':
86 try:
87 main()
88 except Thrift.TException as tx:
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090089 print('%s' % tx.message)