Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (c) 2006- Facebook |
| 4 | # Distributed under the Thrift Software License |
| 5 | # |
| 6 | # See accompanying file LICENSE or visit the Thrift site at: |
| 7 | # http://developers.facebook.com/thrift/ |
| 8 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 9 | from TTransport import * |
| 10 | import socket |
| 11 | |
| 12 | class TSocket(TTransportBase): |
| 13 | |
| 14 | """Socket implementation of TTransport base.""" |
| 15 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 16 | def __init__(self, host='localhost', port=9090): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 17 | self.host = host |
Aditya Agarwal | 9bae5e7 | 2007-02-07 02:36:56 +0000 | [diff] [blame] | 18 | self.port = port |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 19 | self.handle = None |
| 20 | |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 21 | def setHandle(self, h): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 22 | self.handle = h |
| 23 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 24 | def isOpen(self): |
Aditya Agarwal | f954f97 | 2007-02-06 01:26:12 +0000 | [diff] [blame] | 25 | return self.handle != None |
| 26 | |
| 27 | def setTimeout(self, ms): |
| 28 | if (self.handle != None): |
| 29 | self.handle.settimeout(ms/1000.00) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 30 | |
| 31 | def open(self): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 32 | try: |
| 33 | self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 34 | self.handle.connect((self.host, self.port)) |
| 35 | except socket.error, e: |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame^] | 36 | raise TTransportException(TTransportException.NOT_OPEN, 'Could not connect to %s:%d' % (self.host, self.port)) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 37 | |
| 38 | def close(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 39 | if self.handle != None: |
| 40 | self.handle.close() |
| 41 | self.handle = None |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 42 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 43 | def read(self, sz): |
| 44 | buff = self.handle.recv(sz) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 45 | if len(buff) == 0: |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 46 | raise TTransportException('TSocket read 0 bytes') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 47 | return buff |
| 48 | |
| 49 | def write(self, buff): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 50 | sent = 0 |
| 51 | have = len(buff) |
| 52 | while sent < have: |
| 53 | plus = self.handle.send(buff) |
| 54 | if plus == 0: |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 55 | raise TTransportException('TSocket sent 0 bytes') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 56 | sent += plus |
| 57 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 58 | |
| 59 | def flush(self): |
| 60 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 61 | |
| 62 | class TServerSocket(TServerTransportBase): |
| 63 | |
| 64 | """Socket implementation of TServerTransport base.""" |
| 65 | |
| 66 | def __init__(self, port): |
| 67 | self.port = port |
| 68 | self.handle = None |
| 69 | |
| 70 | def listen(self): |
| 71 | self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 72 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 73 | if hasattr(self.handle, 'set_timeout'): |
| 74 | self.handle.set_timeout(None) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 75 | self.handle.bind(('', self.port)) |
| 76 | self.handle.listen(128) |
| 77 | |
| 78 | def accept(self): |
| 79 | (client, addr) = self.handle.accept() |
| 80 | result = TSocket() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 81 | result.setHandle(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 82 | return result |
| 83 | |
| 84 | def close(self): |
| 85 | self.handle.close() |
| 86 | self.handle = None |