| 30 |
ashish |
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 |
sys.path.append('../gen-py')
|
|
|
24 |
|
|
|
25 |
from tutorial import Calculator
|
|
|
26 |
from tutorial.ttypes import *
|
|
|
27 |
|
|
|
28 |
from thrift import Thrift
|
|
|
29 |
from thrift.transport import TSocket
|
|
|
30 |
from thrift.transport import TTransport
|
|
|
31 |
from thrift.protocol import TBinaryProtocol
|
|
|
32 |
|
|
|
33 |
try:
|
|
|
34 |
|
|
|
35 |
# Make socket
|
|
|
36 |
transport = TSocket.TSocket('localhost', 9090)
|
|
|
37 |
|
|
|
38 |
# Buffering is critical. Raw sockets are very slow
|
|
|
39 |
transport = TTransport.TBufferedTransport(transport)
|
|
|
40 |
|
|
|
41 |
# Wrap in a protocol
|
|
|
42 |
protocol = TBinaryProtocol.TBinaryProtocol(transport)
|
|
|
43 |
|
|
|
44 |
# Create a client to use the protocol encoder
|
|
|
45 |
client = Calculator.Client(protocol)
|
|
|
46 |
|
|
|
47 |
# Connect!
|
|
|
48 |
transport.open()
|
|
|
49 |
|
|
|
50 |
client.ping()
|
|
|
51 |
print 'ping()'
|
|
|
52 |
|
|
|
53 |
sum = client.add(1,1)
|
|
|
54 |
print '1+1=%d' % (sum)
|
|
|
55 |
|
|
|
56 |
work = Work()
|
|
|
57 |
|
|
|
58 |
work.op = Operation.DIVIDE
|
|
|
59 |
work.num1 = 1
|
|
|
60 |
work.num2 = 0
|
|
|
61 |
|
|
|
62 |
try:
|
|
|
63 |
quotient = client.calculate(1, work)
|
|
|
64 |
print 'Whoa? You know how to divide by zero?'
|
|
|
65 |
except InvalidOperation, io:
|
|
|
66 |
print 'InvalidOperation: %r' % io
|
|
|
67 |
|
|
|
68 |
work.op = Operation.SUBTRACT
|
|
|
69 |
work.num1 = 15
|
|
|
70 |
work.num2 = 10
|
|
|
71 |
|
|
|
72 |
diff = client.calculate(1, work)
|
|
|
73 |
print '15-10=%d' % (diff)
|
|
|
74 |
|
|
|
75 |
log = client.getStruct(1)
|
|
|
76 |
print 'Check log: %s' % (log.value)
|
|
|
77 |
|
|
|
78 |
# Close!
|
|
|
79 |
transport.close()
|
|
|
80 |
|
|
|
81 |
except Thrift.TException, tx:
|
|
|
82 |
print '%s' % (tx.message)
|