()
| 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__': |
no test coverage detected