Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 1 | # |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
Mark Slee | 89e2bb8 | 2007-03-01 00:20:36 +0000 | [diff] [blame] | 19 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 20 | import errno |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 21 | import os |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 22 | import socket |
David Reiss | 73af3b7 | 2010-08-30 21:57:07 +0000 | [diff] [blame] | 23 | import sys |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 24 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 25 | from TTransport import * |
| 26 | |
| 27 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 28 | class TSocketBase(TTransportBase): |
| 29 | def _resolveAddr(self): |
| 30 | if self._unix_socket is not None: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 31 | return [(socket.AF_UNIX, socket.SOCK_STREAM, None, None, |
| 32 | self._unix_socket)] |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 33 | else: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 34 | return socket.getaddrinfo(self.host, |
| 35 | self.port, |
jfarrell | 8bdff94 | 2013-11-25 21:20:28 -0500 | [diff] [blame] | 36 | self._socket_family, |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 37 | socket.SOCK_STREAM, |
| 38 | 0, |
| 39 | socket.AI_PASSIVE | socket.AI_ADDRCONFIG) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 40 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 41 | def close(self): |
| 42 | if self.handle: |
| 43 | self.handle.close() |
| 44 | self.handle = None |
| 45 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 46 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 47 | class TSocket(TSocketBase): |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 48 | """Socket implementation of TTransport base.""" |
| 49 | |
jfarrell | 3979b86 | 2013-11-25 14:47:16 -0500 | [diff] [blame] | 50 | def __init__(self, host='localhost', port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC): |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 51 | """Initialize a TSocket |
| 52 | |
| 53 | @param host(str) The host to connect to. |
| 54 | @param port(int) The (TCP) port to connect to. |
| 55 | @param unix_socket(str) The filename of a unix socket to connect to. |
| 56 | (host and port will be ignored.) |
jfarrell | 3979b86 | 2013-11-25 14:47:16 -0500 | [diff] [blame] | 57 | @param socket_family(int) The socket family to use with this socket. |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 58 | """ |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 59 | self.host = host |
Aditya Agarwal | 9bae5e7 | 2007-02-07 02:36:56 +0000 | [diff] [blame] | 60 | self.port = port |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 61 | self.handle = None |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 62 | self._unix_socket = unix_socket |
| 63 | self._timeout = None |
jfarrell | 3979b86 | 2013-11-25 14:47:16 -0500 | [diff] [blame] | 64 | self._socket_family = socket_family |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 65 | |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 66 | def setHandle(self, h): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 67 | self.handle = h |
| 68 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 69 | def isOpen(self): |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 70 | return self.handle is not None |
Aditya Agarwal | f954f97 | 2007-02-06 01:26:12 +0000 | [diff] [blame] | 71 | |
| 72 | def setTimeout(self, ms): |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 73 | if ms is None: |
| 74 | self._timeout = None |
James Wang | e168d5e | 2007-07-24 23:59:51 +0000 | [diff] [blame] | 75 | else: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 76 | self._timeout = ms / 1000.0 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 77 | |
Bryan Duxbury | 5040911 | 2011-03-21 17:59:49 +0000 | [diff] [blame] | 78 | if self.handle is not None: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 79 | self.handle.settimeout(self._timeout) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 80 | |
| 81 | def open(self): |
Mark Slee | 92195ae | 2007-02-21 05:16:30 +0000 | [diff] [blame] | 82 | try: |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 83 | res0 = self._resolveAddr() |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 84 | for res in res0: |
| 85 | self.handle = socket.socket(res[0], res[1]) |
David Reiss | c16a8f6 | 2007-12-14 23:46:47 +0000 | [diff] [blame] | 86 | self.handle.settimeout(self._timeout) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 87 | try: |
| 88 | self.handle.connect(res[4]) |
jfarrell | d565e2f | 2015-03-18 21:02:47 -0400 | [diff] [blame^] | 89 | except socket.error as e: |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 90 | if res is not res0[-1]: |
| 91 | continue |
| 92 | else: |
| 93 | raise e |
| 94 | break |
jfarrell | d565e2f | 2015-03-18 21:02:47 -0400 | [diff] [blame^] | 95 | except socket.error as e: |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 96 | if self._unix_socket: |
| 97 | message = 'Could not connect to socket %s' % self._unix_socket |
| 98 | else: |
| 99 | message = 'Could not connect to %s:%d' % (self.host, self.port) |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 100 | raise TTransportException(type=TTransportException.NOT_OPEN, |
| 101 | message=message) |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 102 | |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 103 | def read(self, sz): |
David Reiss | 73af3b7 | 2010-08-30 21:57:07 +0000 | [diff] [blame] | 104 | try: |
| 105 | buff = self.handle.recv(sz) |
jfarrell | d565e2f | 2015-03-18 21:02:47 -0400 | [diff] [blame^] | 106 | except socket.error as e: |
David Reiss | 73af3b7 | 2010-08-30 21:57:07 +0000 | [diff] [blame] | 107 | if (e.args[0] == errno.ECONNRESET and |
| 108 | (sys.platform == 'darwin' or sys.platform.startswith('freebsd'))): |
| 109 | # freebsd and Mach don't follow POSIX semantic of recv |
| 110 | # and fail with ECONNRESET if peer performed shutdown. |
| 111 | # See corresponding comment and code in TSocket::read() |
| 112 | # in lib/cpp/src/transport/TSocket.cpp. |
| 113 | self.close() |
| 114 | # Trigger the check to raise the END_OF_FILE exception below. |
| 115 | buff = '' |
| 116 | else: |
| 117 | raise |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 118 | if len(buff) == 0: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 119 | raise TTransportException(type=TTransportException.END_OF_FILE, |
| 120 | message='TSocket read 0 bytes') |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 121 | return buff |
| 122 | |
| 123 | def write(self, buff): |
David Reiss | a043be3 | 2009-05-12 02:17:43 +0000 | [diff] [blame] | 124 | if not self.handle: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 125 | raise TTransportException(type=TTransportException.NOT_OPEN, |
| 126 | message='Transport not open') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 127 | sent = 0 |
| 128 | have = len(buff) |
| 129 | while sent < have: |
| 130 | plus = self.handle.send(buff) |
| 131 | if plus == 0: |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 132 | raise TTransportException(type=TTransportException.END_OF_FILE, |
| 133 | message='TSocket sent 0 bytes') |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 134 | sent += plus |
| 135 | buff = buff[plus:] |
Mark Slee | cde2b61 | 2006-09-03 21:13:07 +0000 | [diff] [blame] | 136 | |
| 137 | def flush(self): |
| 138 | pass |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 139 | |
Bryan Duxbury | 6972041 | 2012-01-03 17:32:30 +0000 | [diff] [blame] | 140 | |
David Reiss | d73255d | 2009-03-24 22:51:02 +0000 | [diff] [blame] | 141 | class TServerSocket(TSocketBase, TServerTransportBase): |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 142 | """Socket implementation of TServerTransport base.""" |
| 143 | |
jfarrell | 3979b86 | 2013-11-25 14:47:16 -0500 | [diff] [blame] | 144 | def __init__(self, host=None, port=9090, unix_socket=None, socket_family=socket.AF_UNSPEC): |
Bryan Duxbury | e0498c9 | 2011-01-26 17:54:02 +0000 | [diff] [blame] | 145 | self.host = host |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 146 | self.port = port |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 147 | self._unix_socket = unix_socket |
jfarrell | 3979b86 | 2013-11-25 14:47:16 -0500 | [diff] [blame] | 148 | self._socket_family = socket_family |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 149 | self.handle = None |
Mark Slee | 256bdc4 | 2007-11-27 08:42:19 +0000 | [diff] [blame] | 150 | |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 151 | def listen(self): |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 152 | res0 = self._resolveAddr() |
jfarrell | 3979b86 | 2013-11-25 14:47:16 -0500 | [diff] [blame] | 153 | socket_family = self._socket_family == socket.AF_UNSPEC and socket.AF_INET6 or self._socket_family |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 154 | for res in res0: |
jfarrell | 3979b86 | 2013-11-25 14:47:16 -0500 | [diff] [blame] | 155 | if res[0] is socket_family or res is res0[-1]: |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 156 | break |
| 157 | |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 158 | # We need remove the old unix socket if the file exists and |
| 159 | # nobody is listening on it. |
| 160 | if self._unix_socket: |
| 161 | tmp = socket.socket(res[0], res[1]) |
| 162 | try: |
| 163 | tmp.connect(res[4]) |
jfarrell | d565e2f | 2015-03-18 21:02:47 -0400 | [diff] [blame^] | 164 | except socket.error as err: |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 165 | eno, message = err.args |
| 166 | if eno == errno.ECONNREFUSED: |
| 167 | os.unlink(res[4]) |
| 168 | |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 169 | self.handle = socket.socket(res[0], res[1]) |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 170 | self.handle.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
Jake Farrell | 1290d42 | 2011-10-26 12:36:17 +0000 | [diff] [blame] | 171 | if hasattr(self.handle, 'settimeout'): |
| 172 | self.handle.settimeout(None) |
Mark Slee | 2297460 | 2007-07-06 22:20:19 +0000 | [diff] [blame] | 173 | self.handle.bind(res[4]) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 174 | self.handle.listen(128) |
| 175 | |
| 176 | def accept(self): |
David Reiss | e29995e | 2008-07-31 20:15:17 +0000 | [diff] [blame] | 177 | client, addr = self.handle.accept() |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 178 | result = TSocket() |
Mark Slee | 4f0fed6 | 2006-10-02 17:50:08 +0000 | [diff] [blame] | 179 | result.setHandle(client) |
Mark Slee | c967656 | 2006-09-05 17:34:52 +0000 | [diff] [blame] | 180 | return result |