def recv_into(self, buffer, nbytes=None, flags=0): if buffer and (nbytes is None): nbytes = len(buffer) elif nbytes is None: nbytes = 1024 if self._sslobj: if flags != 0: raise ValueError("non-zero flags not allowed in calls to recv_into() on %s" % self.__class__) while True: try: tmp_buffer = self.read(nbytes) v = len(tmp_buffer) buffer[:v] = tmp_buffer return v except SSLError: x = sys.exc_info()[1] if x.args[0] == SSL_ERROR_WANT_READ: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=socket_timeout('timed out')) continue else: raise else: return socket.recv_into(self, buffer, nbytes, flags)
def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT, source_address=None): """Connect to *address* and return the socket object. Convenience function. Connect to *address* (a 2-tuple ``(host, port)``) and return the socket object. Passing the optional *timeout* parameter will set the timeout on the socket instance before attempting to connect. If no *timeout* is supplied, the global default timeout setting returned by :func:`getdefaulttimeout` is used. If *source_address* is set it must be a tuple of (host, port) for the socket to bind as a source address before making the connection. An host of '' or port 0 tells the OS to use the default. """ host, port = address err = None for res in getaddrinfo(host, port, 0 if has_ipv6 else AF_INET, SOCK_STREAM): af, socktype, proto, _canonname, sa = res sock = None try: sock = socket(af, socktype, proto) if timeout is not _GLOBAL_DEFAULT_TIMEOUT: sock.settimeout(timeout) if source_address: sock.bind(source_address) sock.connect(sa) return sock except error: err = sys.exc_info()[1] six.exc_clear() if sock is not None: sock.close() if err is not None: raise err else: raise error("getaddrinfo returns an empty list")
def send(self, data, flags=0): if self._sslobj: if flags != 0: raise ValueError("non-zero flags not allowed in calls to send() on %s" % self.__class__) while True: try: v = self._sslobj.write(data) except SSLError: x = sys.exc_info()[1] if x.args[0] == SSL_ERROR_WANT_READ: if self.timeout == 0.0: return 0 six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=socket_timeout('timed out')) elif x.args[0] == SSL_ERROR_WANT_WRITE: if self.timeout == 0.0: return 0 six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=socket_timeout('timed out')) else: raise else: return v else: return socket.send(self, data, flags)
def recv_into(self, *args): sock = self._sock while True: try: return sock.recv_into(*args) except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: raise del ex six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=timeout("timed out"))
def recv(self, *args): sock = self._sock # keeping the reference so that fd is not closed during waiting while True: try: return sock.recv(*args) except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: raise del ex six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=timeout("timed out"))
def recv(self, *args): sock = self._sock # keeping the reference so that fd is not closed during waiting while True: try: return sock.recv(*args) except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: raise del ex six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=timeout('timed out'))
def recv_into(self, *args): sock = self._sock while True: try: return sock.recv_into(*args) except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: raise del ex six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=timeout('timed out'))
def accept(self): sock = self._sock while True: try: if six.PY3: fd, address = sock._accept() client_socket = _realsocket(self.family, self.type, self.proto, fileno=fd) else: client_socket, address = sock.accept() break except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: raise del ex six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=timeout("timed out")) return socket(_sock=client_socket), address
def send(self, data, flags=0): sock = self._sock try: return sock.send(data, flags) except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: raise del ex six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=timeout('timed out')) try: return sock.send(data, flags) except error: ex = sys.exc_info()[1] if ex.args[0] == EWOULDBLOCK: return 0 raise
def sendto(self, *args): sock = self._sock try: return sock.sendto(*args) except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or timeout == 0.0: raise del ex six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=timeout("timed out")) try: return sock.sendto(*args) except error: ex = sys.exc_info()[1] if ex.args[0] == EWOULDBLOCK: return 0 raise
def sendto(self, *args): sock = self._sock try: return sock.sendto(*args) except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or timeout == 0.0: raise del ex six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=timeout('timed out')) try: return sock.sendto(*args) except error: ex = sys.exc_info()[1] if ex.args[0] == EWOULDBLOCK: return 0 raise
def do_handshake(self): """Perform a TLS/SSL handshake.""" while True: try: return self._sslobj.do_handshake() except SSLError: ex = sys.exc_info()[1] if ex.args[0] == SSL_ERROR_WANT_READ: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=_SSLErrorHandshakeTimeout) elif ex.args[0] == SSL_ERROR_WANT_WRITE: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=_SSLErrorHandshakeTimeout) else: raise
def write(self, data): """Write DATA to the underlying SSL channel. Returns number of bytes of DATA actually transmitted.""" while True: try: return self._sslobj.write(data) except SSLError: ex = sys.exc_info()[1] if ex.args[0] == SSL_ERROR_WANT_READ: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout) elif ex.args[0] == SSL_ERROR_WANT_WRITE: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout) else: raise
def _sslobj_shutdown(self): while True: try: return self._sslobj.shutdown() except SSLError: ex = sys.exc_info()[1] if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs: return b'' elif ex.args[0] == SSL_ERROR_WANT_READ: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout) elif ex.args[0] == SSL_ERROR_WANT_WRITE: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=_SSLErrorWriteTimeout) else: raise
def accept(self): sock = self._sock while True: try: if six.PY3: fd, address = sock._accept() client_socket = _realsocket(self.family, self.type, self.proto, fileno=fd) else: client_socket, address = sock.accept() break except error: ex = sys.exc_info()[1] if ex.args[0] != EWOULDBLOCK or self.timeout == 0.0: raise del ex six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=timeout('timed out')) return socket(_sock=client_socket), address
def read(self, len=1024): """Read up to LEN bytes and return them. Return zero-length string on EOF.""" while True: try: return self._sslobj.read(len) except SSLError: ex = sys.exc_info()[1] if ex.args[0] == SSL_ERROR_EOF and self.suppress_ragged_eofs: return b'' elif ex.args[0] == SSL_ERROR_WANT_READ: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_read(timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout) elif ex.args[0] == SSL_ERROR_WANT_WRITE: if self.timeout == 0.0: raise six.exc_clear() self._io.wait_write(timeout=self.timeout, timeout_exc=_SSLErrorReadTimeout) else: raise