def _read(self, n, initial=False, _errnos=(errno.ENOENT, errno.EAGAIN, errno.EINTR)): # According to SSL_read(3), it can at most return 16kb of data. # Thus, we use an internal read buffer like TCPTransport._read # to get the exact number of bytes wanted. recv = self._quick_recv rbuf = self._read_buffer try: while len(rbuf) < n: try: s = recv(n - len(rbuf)) # see note above except socket.error as exc: # ssl.sock.read may cause a SSLerror without errno # http://bugs.python.org/issue10272 if isinstance(exc, SSLError) and 'timed out' in str(exc): raise socket.timeout() # ssl.sock.read may cause ENOENT if the # operation couldn't be performed (Issue celery#1414). if exc.errno in _errnos: if initial and self.raise_on_initial_eintr: raise socket.timeout() continue raise if not s: raise IOError('Server unexpectedly closed connection') rbuf += s except: # noqa self._read_buffer = rbuf raise result, self._read_buffer = rbuf[:n], rbuf[n:] return result
def sendall(self, data, flags=0): """ - Use select() to simulate a socket timeout without setting the socket to non-blocking mode. - Don't use pyOpenSSL's sendall() either, since it just loops on WantRead or WantWrite, consuming 100% CPU, and never times out. """ timeout = self.__dict__["timeout"] con = self.__dict__["conn"] (read, write, excpt) = select.select([], [con], [], timeout) if not con in write: raise socket.timeout((110, "Operation timed out.")) starttime = time.time() origlen = len(data) sent = -1 while len(data): curtime = time.time() if curtime - starttime > timeout: raise socket.timeout((110, "Operation timed out.")) try: sent = con.send(data, flags) except SSL.SysCallError, e: if e[0] == 32: # Broken Pipe self.close() sent = 0 else: raise socket.error(e) except (SSL.WantWriteError, SSL.WantReadError): time.sleep(0.2) continue
def _check_timeout(self): self._lock() conns = self._sock_dict.values() self._unlock() now = self.get_time() # the socking listening is not in _sock_dict, so it'll not be touched self._last_checktimeout = now for conn in conns: inact_time = now - conn.last_ts if conn.status_rd == ConnState.IDLE and self._idle_timeout > 0 and inact_time > self._idle_timeout: if callable(conn.idle_timeout_cb): conn.error = socket.timeout("idle timeout") conn.idle_timeout_cb(conn, *conn.readable_cb_args) self.close_conn(conn) elif conn.status_rd == ConnState.TOREAD and self._rw_timeout > 0 and inact_time > self._rw_timeout: if callable(conn.read_err_cb): conn.error = socket.timeout("read timeout") self._exec_callback( conn.read_err_cb, (conn,) + conn.read_cb_args, conn.read_tb) self.close_conn(conn) elif conn.status_wr == ConnState.TOWRITE and self._rw_timeout > 0 and inact_time > self._rw_timeout: if callable(conn.write_err_cb): conn.error = socket.timeout("write timeout") self._exec_callback( conn.write_err_cb, (conn,) + conn.write_cb_args, conn.write_tb) self.close_conn(conn)
def connect_ex(self, address): if self.act_non_blocking: return self.fd.connect_ex(address) fd = self.fd if self.gettimeout() is None: while not socket_connect(fd, address): try: self._trampoline(fd, write=True) socket_checkerr(fd) except socket.error as ex: return get_errno(ex) except IOClosed: return errno.EBADFD else: end = time.time() + self.gettimeout() while True: try: if socket_connect(fd, address): return 0 if time.time() >= end: raise socket.timeout(errno.EAGAIN) self._trampoline(fd, write=True, timeout=end - time.time(), timeout_exc=socket.timeout(errno.EAGAIN)) socket_checkerr(fd) except socket.error as ex: return get_errno(ex) except IOClosed: return errno.EBADFD
def test_new_crash_with_fail_retry(self): config = self._setup_config() config.transaction_executor_class = \ TransactionExecutorWithInfiniteBackoff crash_store = RabbitMQCrashStorage(config) iterable = [ ('1', '1', 'crash_id'), timeout(), timeout(), ('2', '2', 'other_id') ] def an_iterator(queue): item = iterable.pop() if isinstance(item, Exception): raise item return item crash_store.rabbitmq.operational_exceptions = ( timeout, ) crash_store.rabbitmq.return_value.__enter__.return_value \ .channel.basic_get = MagicMock(side_effect=an_iterator) expected = ('other_id', 'crash_id', ) for expected, result in zip(expected, crash_store.new_crashes()): eq_(expected, result)
def chk_con(kms): #parse kms hostname = None port = None result = None ip = None ipaddr = None hostname = re.search('https://(.*?):',kms[0],flags=0).group(1) logging.info('KMS FQDN : %s' %hostname) port = re.search('https://.*:(.*)/',kms[0],flags=0).group(1) logging.info ('KMS WebAPI Port : %s' %port) #DNS resolve try: socket.gethostbyname(hostname) except : logging.error('DNS resolve problem for this KMS server FQDN : %s' %hostname) print '!!! DNS resolve issue for FQDN : %s!!!' %hostname return (False) else : result = socket.gethostbyaddr(hostname) ip = result[2] ipaddr = ip[0] logging.info('KMS IP address : %s' %ipaddr) #TCP connect socket.timeout(30) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((ipaddr,int(port))) except Exception, e: logging.exception('TCP connection error with %s:%d. Exception type is %s' % (ipaddr, int(port), `e`)) print 'TCP connection error with KMS server %s port %d. Error type %s'% (ipaddr, int(port), `e`) return (False)
def connect(self, address): if self.act_non_blocking: return self.fd.connect(address) fd = self.fd if self.gettimeout() is None: while not socket_connect(fd, address): try: self._trampoline(fd, write=True) except IOClosed: raise socket.error(errno.EBADFD) socket_checkerr(fd) else: end = time.time() + self.gettimeout() while True: if socket_connect(fd, address): return if time.time() >= end: raise socket.timeout("timed out") try: self._trampoline(fd, write=True, timeout=end - time.time(), timeout_exc=socket.timeout("timed out")) except IOClosed: # ... we need some workable errno here. raise socket.error(errno.EBADFD) socket_checkerr(fd)
def _check_timeout (self): self._lock () conns = self._sock_dict.values () self._unlock () now = self.get_time () #the socking listening is not in _sock_dict, so it'll not be touched self._last_checktimeout = now def __is_timeout (conn): inact_time = now - conn.last_ts if conn.status == ConnState.IDLE and self._idle_timeout > 0 and inact_time > self._idle_timeout: return True elif (conn.status == ConnState.TOREAD or conn.status == ConnState.TOWRITE) \ and self._rw_timeout > 0 and inact_time > self._rw_timeout: return True return False timeout_list = filter (__is_timeout, conns) for conn in timeout_list: if conn.status == ConnState.IDLE: if callable (conn.idle_timeout_cb): conn.error = socket.timeout ("idle timeout") conn.idle_timeout_cb (conn, *conn.readable_cb_args) elif callable(conn.unblock_err_cb): conn.error = socket.timeout ("timeout") self._exec_callback (conn.unblock_err_cb, (conn,) + conn.unblock_cb_args, conn.unblock_tb) self._close_conn (conn)
def recv(self, bufsize, flags=0): """ Use select() to simulate a socket timeout without setting the socket to non-blocking mode """ timeout = self.__dict__["timeout"] con = self.__dict__["conn"] (read, write, excpt) = select.select([con], [], [], timeout) if not con in read: raise socket.timeout((110, "Operation timed out.")) starttime = time.time() while True: curtime = time.time() if curtime - starttime > timeout: raise socket.timeout((110, "Operation timed out.")) try: return con.recv(bufsize, flags) except SSL.ZeroReturnError: return None except SSL.WantReadError: time.sleep(0.2) except Exception, e: if canIgnoreSSLError(e): return None else: raise e
def blocking_read(self, timeout=None): read_frame = self.transport.read_frame if timeout is None: return self.on_inbound_frame(read_frame()) # XXX use select sock = self.sock prev = sock.gettimeout() if prev != timeout: sock.settimeout(timeout) try: try: frame = read_frame() except SSLError as exc: # http://bugs.python.org/issue10272 if 'timed out' in str(exc): raise socket.timeout() # Non-blocking SSL sockets can throw SSLError if 'The operation did not complete' in str(exc): raise socket.timeout() raise else: self.on_inbound_frame(frame) finally: if prev != timeout: sock.settimeout(prev)
def having_timeout(self, timeout): if timeout is None: yield self.sock else: sock = self.sock prev = sock.gettimeout() if prev != timeout: sock.settimeout(timeout) try: yield self.sock except SSLError as exc: if 'timed out' in str(exc): # http://bugs.python.org/issue10272 raise socket.timeout() elif 'The operation did not complete' in str(exc): # Non-blocking SSL sockets can throw SSLError raise socket.timeout() raise except socket.error as exc: if get_errno(exc) == errno.EWOULDBLOCK: raise socket.timeout() raise finally: if timeout != prev: sock.settimeout(prev)
def _safe_ssl_call(suppress_ragged_eofs, sock, call, *args, **kwargs): """Wrap the given call with SSL error-trapping.""" start = time.time() while True: try: return getattr(sock, call)(*args, **kwargs) except (ossl.WantReadError, ossl.WantWriteError): if select is None: if time.time() - start > sock.gettimeout(): raise socket.timeout() time.sleep(SSL_RETRY) elif not select.select([sock], [], [], sock.gettimeout())[0]: raise socket.timeout() except ossl.SysCallError as e: if suppress_ragged_eofs and e.args[0] == (-1, 'Unexpected EOF'): return b'' elif e.args[0] == 0: raise SSLEOFError(*e.args) raise SSLSysCallError(*e.args) except ossl.ZeroReturnError as e: raise SSLZeroReturnError(*e.args) except ossl.Error as e: raise SSLError(*e.args)
def recv(self, nbytes): """ Receive data from the channel. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by C{nbytes}. If a string of length zero is returned, the channel stream has closed. @param nbytes: maximum number of bytes to read. @type nbytes: int @return: data. @rtype: str @raise socket.timeout: if no data is ready before the timeout set by L{settimeout}. """ out = '' self.lock.acquire() try: if len(self.in_buffer) == 0: if self.closed or self.eof_received: return out # should we block? if self.timeout == 0.0: raise socket.timeout() # loop here in case we get woken up but a different thread has grabbed everything in the buffer timeout = self.timeout while (len(self.in_buffer) == 0) and not self.closed and not self.eof_received: then = time.time() self.in_buffer_cv.wait(timeout) if timeout != None: timeout -= time.time() - then if timeout <= 0.0: raise socket.timeout() # something in the buffer and we have the lock if len(self.in_buffer) <= nbytes: out = self.in_buffer self.in_buffer = '' if self.pipe is not None: # clear the pipe, since no more data is buffered self.pipe.clear() else: out = self.in_buffer[:nbytes] self.in_buffer = self.in_buffer[nbytes:] ack = self._check_add_window(len(out)) finally: self.lock.release() # no need to hold the channel lock when sending this if ack > 0: m = Message() m.add_byte(chr(MSG_CHANNEL_WINDOW_ADJUST)) m.add_int(self.remote_chanid) m.add_int(ack) self.transport._send_user_message(m) return out
def HttpPostData(postdata): socket.timeout(HTTPTIMEOUT) try: headers = {"Content-type": "application/json", "Accept": "application/json", "Authorization": AUTHTOKEN} req = urllib2.Request(SOSSERVER + SOSJSON, postdata, headers) response = urllib2.urlopen(req) return response.read() except urllib2.HTTPError as e: print ' error: ' + str(e.code) return "ERROR:HTTP " + str(e.code)
def getTime(): TIME_1970 = 2208988800L client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) socket.timeout(5) data = '\x1b' + 47 * '\0' client.sendto(data, (TimeServer, Port)) data, address = client.recvfrom(1024) data_result = struct.unpack('!12I', data)[10] data_result -= TIME_1970 return data_result
def test_connection(self, proxy): socket.timeout(30) try: http = socket.socket(socket.AF_INET, socket.SOCK_STREAM) http.connect((proxy[0], int(proxy[1]))) request = "GET /search?output=search&q=habrahabr HTTP/1.1\r\n" request += "Host: https://www.google.com\r\n" http.send(request.encode("utf-8", "strict")) http.recv(65535) return True except socket.error: return False
def _recv(self, length): if self._timeout: if hasattr(select, 'poll'): poller = select.poll() poller.register(self._sock.fileno()) ready = poller.poll(self._timeout) if not ready: raise socket.timeout("timed out (poll)") else: ready = select.select([self._sock], [], [], self._timeout) if not ready[0]: raise socket.timeout("timed out (select)") return self._sock.recv(length)
def _connect(self): self.usage = 0 try: socket.timeout(self._timeout) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) s.connect((self._host, self._port)) self._stream = iostream.IOStream(s) self._stream.set_close_callback(self._socket_close) self._connected = True except socket.error, error: raise InterfaceError(error)
def open_socket(): port = 9999 mpstate.status.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: mpstate.status.sock.connect(('localhost', port)) mpstate.status.socket_open = True print("connecting to socket: {}\n".format(port)) except socket.error: if not mpstate.status.sock_failure_data: print("socket not availible\n") mpstate.status.socket_open = False socket.timeout(0.1) time.sleep(0.2)
def recv_stderr(self, nbytes): """ Receive data from the channel's stderr stream. Only channels using L{exec_command} or L{invoke_shell} without a pty will ever have data on the stderr stream. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by C{nbytes}. If a string of length zero is returned, the channel stream has closed. @param nbytes: maximum number of bytes to read. @type nbytes: int @return: data. @rtype: str @raise socket.timeout: if no data is ready before the timeout set by L{settimeout}. @since: 1.1 """ out = '' self.lock.acquire() try: if len(self.in_stderr_buffer) == 0: if self.closed or self.eof_received: return out # should we block? if self.timeout == 0.0: raise socket.timeout() # loop here in case we get woken up but a different thread has grabbed everything in the buffer timeout = self.timeout while (len(self.in_stderr_buffer) == 0) and not self.closed and not self.eof_received: then = time.time() self.in_stderr_buffer_cv.wait(timeout) if timeout != None: timeout -= time.time() - then if timeout <= 0.0: raise socket.timeout() # something in the buffer and we have the lock if len(self.in_stderr_buffer) <= nbytes: out = self.in_stderr_buffer self.in_stderr_buffer = '' else: out = self.in_stderr_buffer[:nbytes] self.in_stderr_buffer = self.in_stderr_buffer[nbytes:] self._check_add_window(len(out)) finally: self.lock.release() return out
def testRecvTimeout(self): sock = ws.WebSocket() s = sock.sock = SockMock() s.add_packet(six.b("\x81")) s.add_packet(socket.timeout()) s.add_packet(six.b("\x8dabcd\x29\x07\x0f\x08\x0e")) s.add_packet(socket.timeout()) s.add_packet(six.b("\x4e\x43\x33\x0e\x10\x0f\x00\x40")) with self.assertRaises(ws.WebSocketTimeoutException): data = sock.recv() with self.assertRaises(ws.WebSocketTimeoutException): data = sock.recv() data = sock.recv() self.assertEqual(data, "Hello, World!") with self.assertRaises(ws.WebSocketConnectionClosedException): data = sock.recv()
def recv(self, count): if sys.stdin.closed: return b'' r, w, x = select.select([sys.stdin], [], [], self.timeout) if sys.stdin in r: return os.read(sys.stdin.fileno(), count) raise socket.timeout()
def read_frame(self, unpack=unpack): read = self._read read_frame_buffer = EMPTY_BUFFER try: frame_header = read(7, True) read_frame_buffer += frame_header frame_type, channel, size = unpack('>BHI', frame_header) payload = read(size) read_frame_buffer += payload ch = ord(read(1)) except socket.timeout: self._read_buffer = read_frame_buffer + self._read_buffer raise except (OSError, IOError, socket.error) as exc: # Don't disconnect for ssl read time outs # http://bugs.python.org/issue10272 if isinstance(exc, SSLError) and 'timed out' in str(exc): raise socket.timeout() if get_errno(exc) not in _UNAVAIL: self.connected = False raise if ch == 206: # '\xce' return frame_type, channel, payload else: raise UnexpectedFrame( 'Received {0:#04x} while expecting 0xce'.format(ch))
def test_url_timeout(self): reason = socket.timeout('too slow') self.urlopen_mock.side_effect = urllib2.URLError(reason) tasks.fetch_manifest('url', self.upload.pk) self.check_validation( 'No manifest was found at that URL. Check the address and try ' 'again.')
def recv(self, nbytes): """ Receive data from the channel. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by ``nbytes``. If a string of length zero is returned, the channel stream has closed. :param int nbytes: maximum number of bytes to read. :return: received data, as a `bytes` :raises socket.timeout: if no data is ready before the timeout set by `settimeout`. """ try: out = self.in_buffer.read(nbytes, self.timeout) except PipeTimeout: raise socket.timeout() ack = self._check_add_window(len(out)) # no need to hold the channel lock when sending this if ack > 0: m = Message() m.add_byte(cMSG_CHANNEL_WINDOW_ADJUST) m.add_int(self.remote_chanid) m.add_int(ack) self.transport._send_user_message(m) return out
def recv_stderr(self, nbytes): """ Receive data from the channel's stderr stream. Only channels using `exec_command` or `invoke_shell` without a pty will ever have data on the stderr stream. The return value is a string representing the data received. The maximum amount of data to be received at once is specified by ``nbytes``. If a string of length zero is returned, the channel stream has closed. :param int nbytes: maximum number of bytes to read. :return: received data as a `str` :raises socket.timeout: if no data is ready before the timeout set by `settimeout`. .. versionadded:: 1.1 """ try: out = self.in_stderr_buffer.read(nbytes, self.timeout) except PipeTimeout: raise socket.timeout() ack = self._check_add_window(len(out)) # no need to hold the channel lock when sending this if ack > 0: m = Message() m.add_byte(cMSG_CHANNEL_WINDOW_ADJUST) m.add_int(self.remote_chanid) m.add_int(ack) self.transport._send_user_message(m) return out
def _recvfrom(self): ret = (None, None) try: if self._interval_finished( interval=self._wakeupInterval, last=self._lastWakeup, ): raise socket.timeout() self._socket.settimeout( max( self._wakeupInterval - self._total_seconds( datetime.datetime.utcnow(), self._lastWakeup ), 1, ) ) ret = self._socket.recvfrom(self._BUF_SIZE) except socket.timeout: self._lastWakeup = datetime.datetime.utcnow() return ret
def drain_events(self, connection, timeout=None): loop = 0 time_start = time() get = self.cycle.get polling_interval = self.polling_interval while 1: try: item, channel = get(timeout=timeout) except Empty: if timeout and time() - time_start >= timeout: raise socket.timeout() loop += 1 if polling_interval is not None: sleep(polling_interval) else: break message, queue = item if not queue or queue not in self._callbacks: raise KeyError( "Received message for queue '%s' without consumers: %s" % ( queue, message)) self._callbacks[queue](message)
def waitSocket(self, sock): fileno = sock.fileno() n = 0 while not n: if time.time() >= self.timeouttime: raise socket.timeout("timed out") n = select.select([fileno], (), (), self.timeouttime - time.time())
def wrap_socket(self, sock, server_side=False, do_handshake_on_connect=True, suppress_ragged_eofs=True, server_hostname=None): cnx = OpenSSL.SSL.Connection(self._ctx, sock) if isinstance(server_hostname, six.text_type): # Platform-specific: Python 3 server_hostname = server_hostname.encode('utf-8') if server_hostname is not None: cnx.set_tlsext_host_name(server_hostname) cnx.set_connect_state() while True: try: cnx.do_handshake() except OpenSSL.SSL.WantReadError: rd = util.wait_for_read(sock, sock.gettimeout()) if not rd: raise timeout('select timed out') continue except OpenSSL.SSL.Error as e: raise ssl.SSLError('bad handshake: %r' % e) break return WrappedSocket(cnx, sock)
def readline(self, timeout=None): try: msg = self.proc_com.outq.get(timeout=timeout) except Empty: raise socket.timeout() return msg
def get_response(self, timeout=None): rstr = self.engine.readline(timeout=timeout) if rstr == "": raise socket.timeout() return self._parse_resp(rstr)
def se(*args, **kwargs): c.should_stop = True raise socket.timeout()
def side_effect(url, timeout=None): raise socket.timeout()
def custom_raise(*_): if self.raised: return 1 self.raised = True raise socket.timeout()
def accept(self): # pylint: disable=no-self-use if not connections: raise socket.timeout() return connections.pop(), 'address'
def side_effect(*args, **kwargs): if acquire_mock.drain_events.call_count == 0: raise socket.timeout() raise TestException()
thirdarg = None try: thirdarg = e.args[0][0][2] except IndexError: pass if thirdarg == 'http request': # The client is talking HTTP to an HTTPS server. raise wsgiserver.NoSSLError() raise wsgiserver.FatalSSLAlert(*e.args) except: raise if time.time() - start > self.ssl_timeout: raise socket.timeout("timed out") def recv(self, size): return self._safe_call(True, super(SSL_fileobject, self).recv, size) def sendall(self, *args, **kwargs): return self._safe_call(False, super(SSL_fileobject, self).sendall, *args, **kwargs) def send(self, *args, **kwargs): return self._safe_call(False, super(SSL_fileobject, self).send, *args, **kwargs)
def _search(self, title, filepath, langs, season, episode, tvshow, year): raise socket.timeout()
def recv_exit_status(self): event = self.exec_channel.status_event status = event.wait(self.client.timeout) if status == False: raise socket.timeout() return self.exec_channel.exit_status
def socket_timeout(err): raise socket.timeout(err)
def drain_events(self, **kwargs): if not self.calls: self.calls += 1 raise socket.timeout() self.obj.connection = None controller.box._node_shutdown.set()
def accept(self): # pylint: disable=no-self-use if connections: return connections.pop(), 'address' quit_event.set() raise socket.timeout()
def drain_events(self, **kwargs): if self.should_raise_timeout: raise socket.timeout() for consumer in MockConsumer.consumers: for callback in consumer.callbacks: callback('body', 'message')
def _wait(self, readable, writable, read_timeout=None): assert readable or writable if not self._wait_for_socket( self._sock, read=readable, write=writable, timeout=read_timeout): raise socket.timeout() # XX use a backend-agnostic exception
def test_socket_error(self): sp = servermanager.ServerPool() with mock.patch(HTTPCON) as conmock: conmock.return_value.request.side_effect = socket.timeout() resp = sp.servers[0].rest_call('GET', '/') self.assertEqual(resp, (0, None, None, None))
) <<<<<<< HEAD result = Security.SSLSetCertificate( self.context, self._client_cert_chain ) ======= result = Security.SSLSetCertificate(self.context, self._client_cert_chain) >>>>>>> development _assert_no_error(result) while True: with self._raise_on_error(): result = Security.SSLHandshake(self.context) if result == SecurityConst.errSSLWouldBlock: raise socket.timeout("handshake timed out") elif result == SecurityConst.errSSLServerAuthCompleted: self._custom_validate(verify, trust_bundle) continue else: _assert_no_error(result) break def fileno(self): return self.socket.fileno() # Copy-pasted from Python 3.5 source code def _decref_socketios(self): if self._makefile_refs > 0: self._makefile_refs -= 1 if self._closed:
def read_frame(self, unpack=unpack): """Parse AMQP frame. Frame has following format:: 0 1 3 7 size+7 size+8 +------+---------+---------+ +-------------+ +-----------+ | type | channel | size | | payload | | frame-end | +------+---------+---------+ +-------------+ +-----------+ octet short long 'size' octets octet """ read = self._read read_frame_buffer = EMPTY_BUFFER try: frame_header = read(7, True) read_frame_buffer += frame_header frame_type, channel, size = unpack('>BHI', frame_header) # >I is an unsigned int, but the argument to sock.recv is signed, # so we know the size can be at most 2 * SIGNED_INT_MAX if size > SIGNED_INT_MAX: part1 = read(SIGNED_INT_MAX) try: part2 = read(size - SIGNED_INT_MAX) except (socket.timeout, OSError, SSLError): # In case this read times out, we need to make sure to not # lose part1 when we retry the read read_frame_buffer += part1 raise payload = b''.join([part1, part2]) else: payload = read(size) read_frame_buffer += payload frame_end = ord(read(1)) except socket.timeout: self._read_buffer = read_frame_buffer + self._read_buffer raise except (OSError, SSLError) as exc: if (isinstance(exc, socket.error) and os.name == 'nt' and exc.errno == errno.EWOULDBLOCK # noqa ): # On windows we can get a read timeout with a winsock error # code instead of a proper socket.timeout() error, see # https://github.com/celery/py-amqp/issues/320 self._read_buffer = read_frame_buffer + self._read_buffer raise socket.timeout() if isinstance(exc, SSLError) and 'timed out' in str(exc): # Don't disconnect for ssl read time outs # http://bugs.python.org/issue10272 self._read_buffer = read_frame_buffer + self._read_buffer raise socket.timeout() if exc.errno not in _UNAVAIL: self.connected = False raise # frame-end octet must contain '\xce' value if frame_end == 206: return frame_type, channel, payload else: raise UnexpectedFrame( f'Received frame_end {frame_end:#04x} while expecting 0xce')
def test_socket_error(): sc = _client() sc._sock.sendto.side_effect = socket.timeout() sc.incr('foo') _sock_check(sc, 1, 'foo:1|c')
def test_connection_timeout(self, output_spy, virtual_socket): vsocket = virtual_socket vsocket.errors['connect'] = socket.timeout('timed out') lines = self._audit(output_spy, exit_expected=True) assert len(lines) == 1 assert 'timed out' in lines[-1]
def connect(self, *args): super().connect(*args) if not next(self._connector): raise socket.timeout("mock reconnect timeout")
def test_timeout(mock_urlopen): exc = URLError(reason=socket.timeout('timed_out')) mock_urlopen.side_effect = exc generator = request.stream('http://fakeassurl.gov/timeout_test', timeout=1) with pytest.raises(MaxRetriesExceeded): next(generator)
def wait_port(port, timeout=10): """Wait up to timeout for port to be connectable.""" if timeout: deadline = time.time() + timeout while (timeout is None or time.time() < deadline): try: s = socket.create_connection((None, port), timeout) # Works for IPv6 and v4 s.close() return except socket.error, e: if e.errno != errno.ECONNREFUSED: # Only retry on connection refused error. raise raise socket.timeout() class BrokerTestCase(ExampleTestCase): """ ExampleTest that starts a broker in setUpClass and kills it in tearDownClass. Subclass must set `broker_exe` class variable with the name of the broker executable. """ @classmethod def setUpClass(cls): cls.port = pick_port() cls.addr = "127.0.0.1:%s/examples" % (cls.port) cls.broker = None # In case Proc throws, create the attribute. cls.broker = Proc(cls.broker_exe + ["-a", cls.addr]) try: wait_port(cls.port)
address = os.environ.get('HLDS_ADDRESS', '127.0.0.1') port = int(os.environ.get('HLDS_PORT', 27111)) server_name = os.environ.get('HLDS_NAME', 'Test auto') map_name = os.environ.get('HLDS_MAP', 'de_dust2') game_name = os.environ.get('HLDS_GAME', 'Counter-Strike') sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(3) request_data = 'ffffffff54536f7572636520456e67696e6520517565727900' retries = 10 request_bytes = bytearray(bytes.fromhex(request_data)) while retries: retries -= 1 try: sock.sendto(request_bytes, (address, port)) response_data, remote_addr = sock.recvfrom(1024) except socket.timeout: if not retries: raise socket.timeout('Nothing was received from HLDS') expected = '.*{0}.*{1}.*{2}.*'.format(server_name, map_name, game_name) match = re.match(str.encode(expected, 'ASCII'), response_data) assert match, ("Server discovery test failed!" "Recieved : {0}").format(response_data) print("SUCCESS!")
def handshake( self, server_hostname, verify, trust_bundle, min_version, max_version, client_cert, client_key, client_key_passphrase, ): """ Actually performs the TLS handshake. This is run automatically by wrapped socket, and shouldn't be needed in user code. """ # First, we do the initial bits of connection setup. We need to create # a context, set its I/O funcs, and set the connection reference. self.context = Security.SSLCreateContext(None, SecurityConst.kSSLClientSide, SecurityConst.kSSLStreamType) result = Security.SSLSetIOFuncs(self.context, _read_callback_pointer, _write_callback_pointer) _assert_no_error(result) # Here we need to compute the handle to use. We do this by taking the # id of self modulo 2**31 - 1. If this is already in the dictionary, we # just keep incrementing by one until we find a free space. with _connection_ref_lock: handle = id(self) % 2147483647 while handle in _connection_refs: handle = (handle + 1) % 2147483647 _connection_refs[handle] = self result = Security.SSLSetConnection(self.context, handle) _assert_no_error(result) # If we have a server hostname, we should set that too. if server_hostname: if not isinstance(server_hostname, bytes): server_hostname = server_hostname.encode("utf-8") result = Security.SSLSetPeerDomainName(self.context, server_hostname, len(server_hostname)) _assert_no_error(result) # Setup the ciphers. self._set_ciphers() # Set the minimum and maximum TLS versions. result = Security.SSLSetProtocolVersionMin(self.context, min_version) _assert_no_error(result) # TLS 1.3 isn't necessarily enabled by the OS # so we have to detect when we error out and try # setting TLS 1.3 if it's allowed. kTLSProtocolMaxSupported # was added in macOS 10.13 along with kTLSProtocol13. result = Security.SSLSetProtocolVersionMax(self.context, max_version) if result != 0 and max_version == SecurityConst.kTLSProtocolMaxSupported: result = Security.SSLSetProtocolVersionMax( self.context, SecurityConst.kTLSProtocol12) _assert_no_error(result) # If there's a trust DB, we need to use it. We do that by telling # SecureTransport to break on server auth. We also do that if we don't # want to validate the certs at all: we just won't actually do any # authing in that case. if not verify or trust_bundle is not None: result = Security.SSLSetSessionOption( self.context, SecurityConst.kSSLSessionOptionBreakOnServerAuth, True) _assert_no_error(result) # If there's a client cert, we need to use it. if client_cert: self._keychain, self._keychain_dir = _temporary_keychain() self._client_cert_chain = _load_client_cert_chain( self._keychain, client_cert, client_key) result = Security.SSLSetCertificate(self.context, self._client_cert_chain) _assert_no_error(result) while True: with self._raise_on_error(): result = Security.SSLHandshake(self.context) if result == SecurityConst.errSSLWouldBlock: raise socket.timeout("handshake timed out") elif result == SecurityConst.errSSLServerAuthCompleted: self._custom_validate(verify, trust_bundle) continue else: _assert_no_error(result) break
def first(*args, **kwargs): mock.side_effect = socket.error() raise socket.timeout()
def connect(self, *args): if self.raise_connect_timeout: raise socket.timeout("mock failed socket.connect")
def test_io_receive_raises_socket_timeout(self): connection = FakeConnection() io = IO(connection.parameters) io.socket = mock.Mock(name='socket', spec=socket.socket) io.socket.recv.side_effect = socket.timeout('timeout') io._receive()
def drain_events(self, **kwargs): self.obj.connection = None raise socket.timeout(10)
def read_message(self): self.status.append("I was unable to read data.") raise socket.timeout()