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