我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用errno.ECONNABORTED。
def accept(self, fd): '''??????fd???fd?? ''' try: # ??fd??????????fd? sock_state = self.conn_state[fd] # ??sock??????soket? sock = sock_state.sock_obj # ??accept???????????????????conn?????socket???addr????????? conn, addr = sock.accept() # ??socket???? conn.setblocking(0) # ?epoll??????socket??fd self.epoll_sock.register(conn.fileno(), select.EPOLLIN) # ???????conn???????sock self.setFd(conn) # ????fd????read epoll???????????????? self.conn_state[conn.fileno()].state = "read" except socket.error as msg: # ECONNABORTED??TCP???????????RST # EAGIIN ??????????????????? # ????accept if msg.args[0] in (errno.ECONNABORTED, errno.EAGAIN): return raise
def request(self, host, handler, request_body, verbose=0): #retry request once if cached connection has gone cold for i in (0, 1): try: return self.single_request(host, handler, request_body, verbose) except socket.error, e: if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE): raise except httplib.BadStatusLine: #close after we sent request if i: raise ## # Send a complete request, and parse the response. # # @param host Target host. # @param handler Target PRC handler. # @param request_body XML-RPC request body. # @param verbose Debugging flag. # @return Parsed response.
def handle_one_request(self): if not self.disable_transport_ssl and self.scheme == 'http': leadbyte = self.connection.recv(1, socket.MSG_PEEK) if leadbyte in ('\x80', '\x16'): server_name = '' if leadbyte == '\x16': for _ in xrange(2): leaddata = self.connection.recv(1024, socket.MSG_PEEK) if is_clienthello(leaddata): try: server_name = extract_sni_name(leaddata) finally: break try: certfile = CertUtil.get_cert(server_name or 'www.google.com') ssl_sock = ssl.wrap_socket(self.connection, ssl_version=self.ssl_version, keyfile=certfile, certfile=certfile, server_side=True) except StandardError as e: if e.args[0] not in (errno.ECONNABORTED, errno.ECONNRESET): logging.exception('ssl.wrap_socket(self.connection=%r) failed: %s', self.connection, e) return self.connection = ssl_sock self.rfile = self.connection.makefile('rb', self.bufsize) self.wfile = self.connection.makefile('wb', 0) self.scheme = 'https' return BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request(self)
def run_for_multiple(self, timeout): while self.alive: self.notify() try: ready = self.wait(timeout) except StopWaiting: return if ready is not None: for listener in ready: try: self.accept(listener) except socket.error as e: if e.args[0] not in (errno.EAGAIN, errno.ECONNABORTED, errno.EWOULDBLOCK): raise if not self.is_parent_alive(): return
def run_for_multiple(self, timeout): while self.alive: self.notify() try: ready = self.wait(timeout) except StopWaiting: return if ready is not None: for listener in ready: if listener == self.PIPE[0]: continue try: self.accept(listener) except EnvironmentError as e: if e.errno not in (errno.EAGAIN, errno.ECONNABORTED, errno.EWOULDBLOCK): raise if not self.is_parent_alive(): return
def _buffered_read(self, length): # try to recv and strore needed bytes if self._readbuffer_head + length > len(self._readbuffer): if self._ssl: data = self._socket.read(self._readbuffer_head + length - len(self._readbuffer)) else: data = self._socket.recv(self._readbuffer_head + length - len(self._readbuffer)) if not data: raise socket.error(errno.ECONNABORTED, 0) else: self._readbuffer.extend(data) self._readbuffer_head += length return self._readbuffer[self._readbuffer_head-length:self._readbuffer_head]
def _do_tls_handshake(self): try: self.socket.do_handshake() except ssl.SSLError as err: if err.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE): return elif err.args[0] == ssl.SSL_ERROR_EOF: return self.handle_close() raise except OSError as err: if err.args[0] == errno.ECONNABORTED: return self.handle_close() else: self.tls_active = True self.tls_starting = False
def _buffered_read(self, length): # try to recv and strore needed bytes wanted_bytes = length - (len(self._readbuffer) - self._readbuffer_head) if wanted_bytes > 0: data = self._socket.recv(wanted_bytes) if not data: raise socket.error(errno.ECONNABORTED, 0) else: self._readbuffer.extend(data) if len(data) < wanted_bytes: raise socket.error(errno.EAGAIN, 0) self._readbuffer_head += length return self._readbuffer[self._readbuffer_head - length:self._readbuffer_head]
def handle_accept(self): try: sock, addr = self.accept() except TypeError: # sometimes accept() might return None, see: # https://github.com/giampaolo/pyftpdlib/issues/91 debug("call: handle_accept(); accept() returned None", self) return except socket.error as err: # ECONNABORTED might be thrown on *BSD, see: # https://github.com/giampaolo/pyftpdlib/issues/105 if err.errno != errno.ECONNABORTED: raise else: debug("call: handle_accept(); accept() returned ECONNABORTED", self) else: # sometimes addr == None instead of (ip, port) (see issue 104) if addr is not None: self.handle_accepted(sock, addr)
def forward_socket(local, remote, timeout, bufsize): """forward socket""" def __io_copy(dest, source, timeout): try: dest.settimeout(timeout) source.settimeout(timeout) while 1: data = source.recv(bufsize) if not data: break dest.sendall(data) except socket.timeout: pass except (socket.error, ssl.SSLError, OpenSSL.SSL.Error) as e: if e.args[0] not in (errno.ECONNABORTED, errno.ECONNRESET, errno.ENOTCONN, errno.EPIPE): raise if e.args[0] in (errno.EBADF,): return finally: for sock in (dest, source): try: sock.close() except StandardError: pass thread.start_new_thread(__io_copy, (remote.dup(), local.dup(), timeout)) __io_copy(local, remote, timeout)
def finish_request(self, request, client_address): try: self.RequestHandlerClass(request, client_address, self) except (socket.error, ssl.SSLError, OpenSSL.SSL.Error) as e: if e[0] not in (errno.ECONNABORTED, errno.ECONNRESET, errno.EPIPE): logging.error("error no:%d", e[0]) raise
def handle(self, handler, do_ssl_handshake=True): """strip connect""" logging.info('%s "STRIP %s %s:%d %s" - -', handler.address_string(), handler.command, handler.host, handler.port, handler.protocol_version) handler.send_response(200) handler.end_headers() if do_ssl_handshake: try: self.do_ssl_handshake(handler) except (socket.error, ssl.SSLError, OpenSSL.SSL.Error) as e: if e.args[0] not in (errno.ECONNABORTED, errno.ECONNRESET) or (e[0] == -1 and 'Unexpected EOF' in e[1]): logging.exception('ssl.wrap_socket(connection=%r) failed: %s', handler.connection, e) return try: handler.raw_requestline = handler.rfile.readline(65537) if len(handler.raw_requestline) > 65536: handler.requestline = '' handler.request_version = '' handler.command = '' handler.send_error(414) handler.wfile.close() return if not handler.raw_requestline: handler.close_connection = 1 return if not handler.parse_request(): handler.send_error(400) handler.wfile.close() return except (socket.error, ssl.SSLError, OpenSSL.SSL.Error) as e: if e.args[0] in (errno.ECONNABORTED, errno.ECONNRESET, errno.EPIPE): handler.close_connection = 1 return else: raise try: handler.do_METHOD() except (socket.error, ssl.SSLError, OpenSSL.SSL.Error) as e: if e.args[0] not in (errno.ECONNABORTED, errno.ETIMEDOUT, errno.EPIPE): raise
def finish(self): """make python2 BaseHTTPRequestHandler happy""" try: BaseHTTPServer.BaseHTTPRequestHandler.finish(self) except (socket.error, ssl.SSLError, OpenSSL.SSL.Error) as e: if e[0] not in (errno.ECONNABORTED, errno.ECONNRESET, errno.EPIPE): raise
def run_for_one(self, timeout): listener = self.sockets[0] while self.alive: self.notify() # Accept a connection. If we get an error telling us # that no connection is waiting we fall down to the # select which is where we'll wait for a bit for new # workers to come give us some love. try: self.accept(listener) # Keep processing clients until no one is waiting. This # prevents the need to select() for every client that we # process. continue except socket.error as e: if e.args[0] not in (errno.EAGAIN, errno.ECONNABORTED, errno.EWOULDBLOCK): raise if not self.is_parent_alive(): return try: self.wait(timeout) except StopWaiting: return
def accept(self, listener): try: client, addr = listener.accept() # initialize the connection object conn = TConn(self.cfg, listener, client, addr) self.nr_conns += 1 # enqueue the job self.enqueue_req(conn) except socket.error as e: if e.args[0] not in (errno.EAGAIN, errno.ECONNABORTED, errno.EWOULDBLOCK): raise
def request(self, host, handler, request_body, verbose=False): #retry request once if cached connection has gone cold for i in (0, 1): try: return self.single_request(host, handler, request_body, verbose) except socket.error as e: if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE): raise except http_client.BadStatusLine: #close after we sent request if i: raise
def test_serve_listeners_accept_unrecognized_error(): for error in [KeyError(), OSError(errno.ECONNABORTED, "ECONNABORTED")]: listener = MemoryListener() async def raise_error(): raise error listener.accept_hook = raise_error with pytest.raises(type(error)) as excinfo: await trio.serve_listeners(None, [listener]) assert excinfo.value is error
def handle_accept_socket_exeption(cls, error): if error.errno in (errno.EWOULDBLOCK, errno.EAGAIN): # Try again return True # continue accept loop elif error.errno == errno.EPERM: # Netfilter on Linux may have rejected the # connection, but we get told to try to accept() # anyway. return True # continue accept loop elif error.errno in (errno.EMFILE, errno.ENOBUFS, errno.ENFILE, errno.ENOMEM, errno.ECONNABORTED): # Linux gives EMFILE when a process is not allowed to # allocate any more file descriptors. *BSD and Win32 # give (WSA)ENOBUFS. Linux can also give ENFILE if the # system is out of inodes, or ENOMEM if there is # insufficient memory to allocate a new dentry. # ECONNABORTED is documented as possible on all # relevant platforms (Linux, Windows, macOS, and the # BSDs) but occurs only on the BSDs. It occurs when a # client sends a FIN or RST after the server sends a # SYN|ACK but before application code calls accept(2). # On Linux, calling accept(2) on such a listener # returns a connection that fails as though the it were # terminated after being fully established. This # appears to be an implementation choice (see # inet_accept in inet/ipv4/af_inet.c). On macOS X, # such a listener is not considered readable, so # accept(2) will never be called. Calling accept(2) on # such a listener, however, does not return at all. log.error("Could not accept new connection (%s)" % error.strerror) return False # break accept loop
def test_socket_error_on_accept(self, monkeypatch): if sys.platform != 'win32': monkeypatch.setattr(os, 'fork', lambda: 0) monkeypatch.setattr(socket, 'socket', lambda af, st: self.socket) self.socket.accept = lambda: MockUtils.raise_( socket.error(errno.ECONNABORTED, "error message")) try: RLXPort.listen(('localhost', 7000)) assert False except Exception as e: assert str(e) == '[Errno %d] error message' % errno.ECONNABORTED
def test_socket_error(self, monkeypatch): monkeypatch.setattr(time, 'sleep', lambda x: x) skt = MockSocket.create() skt.connect = lambda: socket.error(errno.ECONNABORTED) try: AgentProxy('localhost:7000').connect() assert False except AgentProxyException as e: if sys.platform == 'win32': assert str(e) == '[WinError %d] No connection could be made because the target machine actively refused it' % errno.ECONNREFUSED else: assert str(e) == '[Errno %d] Connection refused' % errno.ECONNREFUSED