blob: 96ca03a9154f8a2a7a1a2746f5510bbab00387ca [file] [log] [blame]
Mark Slee07a3aab2007-03-07 05:45:10 +00001#!/usr/bin/env python
2
3import sys
4sys.path.append('../gen-py')
5
6from tutorial import Calculator
7from tutorial.ttypes import *
8
9from thrift.transport import TSocket
10from thrift.transport import TTransport
11from thrift.protocol import TBinaryProtocol
12
13# Make socket
14transport = TSocket.TSocket('localhost', 9090)
15
16# Buffering is critical. Raw sockets are very slow
17transport = TTransport.TBufferedTransport(transport)
18
19# Wrap in a protocol
20protocol = TBinaryProtocol.TBinaryProtocol(transport)
21
22# Create a client to use the protocol encoder
23client = Calculator.Client(protocol)
24
25# Connect!
26transport.open()
27
28client.ping()
29print 'ping()'
30
31sum = client.add(1,1)
32print '1+1=%d' % (sum)
33
34work = Work()
35
36work.op = Operation.DIVIDE
37work.num1 = 1
38work.num2 = 0
39
40try:
41 quotient = client.calculate(1, work)
42 print 'Whoa? You know how to divide by zero?'
43except InvalidOperation, io:
44 print 'InvalidOperation: %s' % (io.__str__())
45
46work.op = Operation.SUBTRACT
47work.num1 = 15
48work.num2 = 10
49
50diff = client.calculate(1, work)
51print '15-10=%d' % (diff)
52
53log = client.getStruct(1)
54print 'Check log: %s' % (log.value)
55
56# Close!
57transport.close()
58