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