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