2007-03-07 05:45:10 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2009-03-30 21:35:00 +00:00
|
|
|
#
|
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
|
# or more contributor license agreements. See the NOTICE file
|
|
|
|
|
# distributed with this work for additional information
|
|
|
|
|
# regarding copyright ownership. The ASF licenses this file
|
|
|
|
|
# to you under the Apache License, Version 2.0 (the
|
|
|
|
|
# "License"); you may not use this file except in compliance
|
|
|
|
|
# with the License. You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing,
|
|
|
|
|
# software distributed under the License is distributed on an
|
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
|
# KIND, either express or implied. See the License for the
|
|
|
|
|
# specific language governing permissions and limitations
|
|
|
|
|
# under the License.
|
|
|
|
|
#
|
|
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
import sys
|
|
|
|
|
import glob
|
2012-10-26 21:46:18 +00:00
|
|
|
sys.path.append('gen-py')
|
2015-11-06 21:24:16 +09:00
|
|
|
sys.path.insert(0, glob.glob('../../lib/py/build/lib*')[0])
|
2007-03-07 05:45:10 +00:00
|
|
|
|
|
|
|
|
from tutorial import Calculator
|
2016-01-11 11:25:09 +09:00
|
|
|
from tutorial.ttypes import InvalidOperation, Operation, Work
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2007-03-14 02:47:35 +00:00
|
|
|
from thrift import Thrift
|
2007-03-07 05:45:10 +00:00
|
|
|
from thrift.transport import TSocket
|
|
|
|
|
from thrift.transport import TTransport
|
|
|
|
|
from thrift.protocol import TBinaryProtocol
|
|
|
|
|
|
|
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
def main():
|
|
|
|
|
# Make socket
|
|
|
|
|
transport = TSocket.TSocket('localhost', 9090)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
# Buffering is critical. Raw sockets are very slow
|
|
|
|
|
transport = TTransport.TBufferedTransport(transport)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
# Wrap in a protocol
|
|
|
|
|
protocol = TBinaryProtocol.TBinaryProtocol(transport)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
# Create a client to use the protocol encoder
|
|
|
|
|
client = Calculator.Client(protocol)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
# Connect!
|
|
|
|
|
transport.open()
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
client.ping()
|
|
|
|
|
print('ping()')
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-02-28 11:28:19 +09:00
|
|
|
sum_ = client.add(1, 1)
|
|
|
|
|
print('1+1=%d' % sum_)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
work = Work()
|
2008-02-06 22:18:40 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
work.op = Operation.DIVIDE
|
|
|
|
|
work.num1 = 1
|
|
|
|
|
work.num2 = 0
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
try:
|
|
|
|
|
quotient = client.calculate(1, work)
|
|
|
|
|
print('Whoa? You know how to divide by zero?')
|
|
|
|
|
print('FYI the answer is %d' % quotient)
|
|
|
|
|
except InvalidOperation as e:
|
2016-02-28 11:28:19 +09:00
|
|
|
print('InvalidOperation: %r' % e)
|
2008-02-06 22:18:40 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
work.op = Operation.SUBTRACT
|
|
|
|
|
work.num1 = 15
|
|
|
|
|
work.num2 = 10
|
2008-02-06 22:18:40 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
diff = client.calculate(1, work)
|
2016-02-28 11:28:19 +09:00
|
|
|
print('15-10=%d' % diff)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
log = client.getStruct(1)
|
2016-02-28 11:28:19 +09:00
|
|
|
print('Check log: %s' % log.value)
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
# Close!
|
|
|
|
|
transport.close()
|
2007-03-07 05:45:10 +00:00
|
|
|
|
2017-09-30 15:44:16 -07:00
|
|
|
|
2016-01-11 11:25:09 +09:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
try:
|
|
|
|
|
main()
|
|
|
|
|
except Thrift.TException as tx:
|
2016-02-28 11:28:19 +09:00
|
|
|
print('%s' % tx.message)
|