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