Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # Copyright (c) 2006- Facebook |
| 2 | # Distributed under the Thrift Software License |
| 3 | # |
| 4 | # See accompanying file LICENSE or visit the Thrift site at: |
| 5 | # http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 7 | from TTransport import * |
| 8 | import socket |
| 9 | |
| 10 | class TSocket(TTransportBase): |
| 11 | |
| 12 | """Socket implementation of TTransport base.""" |
| 13 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 14 | def __init__(self, host='localhost', port=9090, unix_socket=None): |
| 15 | """Initialize a TSocket |
| 16 | |
| 17 | @param host(str) The host to connect to. |
| 18 | @param port(int) The (TCP) port to connect to. |
| 19 | @param unix_socket(str) The filename of a unix socket to connect to. |
| 20 | (host and port will be ignored.) |
| 21 | """ |
| 22 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 23 | self.host = host |
Aditya Agarwal | 9bae5e7 | 2007-02-07 02:36:56 +0000 | [diff] [blame] | 24 | self.port = port |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 25 | self.handle = None |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 26 | self._unix_socket = unix_socket |
| 27 | self._timeout = None |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 28 | |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 29 | def setHandle(self, h): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 30 | self.handle = h |
| 31 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 32 | def isOpen(self): |
Aditya Agarwal | f954f97 | 2007-02-06 01:26:12 +0000 | [diff] [blame] | 33 | return self.handle != None |
| 34 | |
| 35 | def setTimeout(self, ms): |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 36 | if ms is None: |
| 37 | self._timeout = None |
James Wang | e168d5e | 2007-07-24 23:59:51 +0000 | [diff] [blame] | 38 | else: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 39 | self._timeout = ms/1000.0 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 40 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 41 | if (self.handle != None): |
| 42 | self.handle.settimeout(self._timeout) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 43 | |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 44 | def _resolveAddr(self): |
| 45 | if self._unix_socket is not None: |
| 46 | return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None, self._unix_socket)] |
| 47 | else: |
| 48 | return socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 49 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 50 | def open(self): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 51 | try: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 52 | res0 = self._resolveAddr() |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 53 | for res in res0: |
| 54 | self.handle = socket.socket(res[0], res[1]) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 55 | self.handle.settimeout(self._timeout) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 56 | try: |
| 57 | self.handle.connect(res[4]) |
| 58 | except socket.error, e: |
| 59 | if res is not res0[-1]: |
| 60 | continue |
| 61 | else: |
| 62 | raise e |
| 63 | break |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 64 | except socket.error, e: |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 65 | 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] | 66 | |
| 67 | def close(self): |
Mark Slee | d788b2e | 2006-09-07 01:26:35 +0000 | [diff] [blame] | 68 | if self.handle != None: |
| 69 | self.handle.close() |
| 70 | self.handle = None |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 71 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 72 | def read(self, sz): |
| 73 | buff = self.handle.recv(sz) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 74 | if len(buff) == 0: |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 75 | raise TTransportException('TSocket read 0 bytes') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 76 | return buff |
| 77 | |
| 78 | def write(self, buff): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 79 | sent = 0 |
| 80 | have = len(buff) |
| 81 | while sent < have: |
| 82 | plus = self.handle.send(buff) |
| 83 | if plus == 0: |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 84 | raise TTransportException('TSocket sent 0 bytes') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 85 | sent += plus |
| 86 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 87 | |
| 88 | def flush(self): |
| 89 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 90 | |
| 91 | class TServerSocket(TServerTransportBase): |
| 92 | |
| 93 | """Socket implementation of TServerTransport base.""" |
| 94 | |
| 95 | def __init__(self, port): |
| 96 | self.port = port |
| 97 | self.handle = None |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 98 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 99 | def listen(self): |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 100 | res0 = socket.getaddrinfo(None, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 101 | for res in res0: |
| 102 | if res[0] is socket.AF_INET6 or res is res0[-1]: |
| 103 | break |
| 104 | |
| 105 | self.handle = socket.socket(res[0], res[1]) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 106 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 107 | if hasattr(self.handle, 'set_timeout'): |
| 108 | self.handle.set_timeout(None) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 109 | self.handle.bind(res[4]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 110 | self.handle.listen(128) |
| 111 | |
| 112 | def accept(self): |
| 113 | (client, addr) = self.handle.accept() |
| 114 | result = TSocket() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 115 | result.setHandle(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 116 | return result |
| 117 | |
| 118 | def close(self): |
| 119 | self.handle.close() |
| 120 | self.handle = None |