我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用errno.ECONNRESET。
def openBrowserRecursively(total, idName, browser): try: for i in range(total): iterator = i * 100 url = r"https://www.google.com/search?q={word}&newwindow=1&biw=300&bih=629&tbm=isch&ijn={times}&start={start}" try: browser.get(url.format(word= idName, start=iterator,times = i)) except SocketError as e: if e.errno != errno.ECONNRESET: raise # raise to reset the connection pass time.sleep(1.5) # 1.5 seconds is the tuned time for HKU service not to be monitored and closed except: if isWindows: os.system("taskkill /im chrome.exe /F") else : os.system("kill " + str(os.getpid())) openBrowserRecursively(total, idName, browser) # basic session setup
def _receiveWithTimeout(self, count): chunk = str() while len(chunk) < count: try: tmp = self.socket.recv(count - len(chunk)) except socket.timeout: if not self._running: print("CLIENT: Socket timed out and termination requested.") return None else: continue except socket.error as e: if e[0] == errno.ECONNRESET: print("CLIENT: Connection reset by peer.") return None else: raise e if not tmp or len(tmp) == 0: print("CLIENT: Socket has been closed.") return None chunk = chunk + tmp return chunk
def _transmitWithTimeout(self, data): sent = 0 while sent < len(data): try: tmp = self.socket.send(data[sent:]) except socket.timeout: if not self._running: print("CLIENT: Socket timed out and termination requested.") return False else: continue except socket.error as e: if e[0] == errno.ECONNRESET: print("CLIENT: Connection reset by peer.") return False else: raise e if tmp == 0: return False sent += tmp return True
def _receiveWithTimeout(self, count): chunk = str() while len(chunk) < count: try: tmp = self.socket.recv(count - len(chunk)) except socket.timeout: if not self._running: print "CLIENT: Socket timed out and termination requested." return None else: continue except socket.error, e: if e[0] == errno.ECONNRESET: print "CLIENT: Connection reset by peer." return None else: raise e if not tmp or len(tmp) == 0: print "CLIENT: Socket has been closed." return None chunk = chunk + tmp return chunk
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 test_serialized_with_retry_socket_error_conn_reset(self): exc = socket.error() exc.errno = errno.ECONNRESET plugin = 'glance' fn = 'download_vhd' num_retries = 1 callback = None retry_cb = mock.Mock() with mock.patch.object(self.session, 'call_plugin_serialized', spec=True) as call_plugin_serialized: call_plugin_serialized.side_effect = exc self.assertRaises( exception.PluginRetriesExceeded, self.session.call_plugin_serialized_with_retry, plugin, fn, num_retries, callback, retry_cb) call_plugin_serialized.assert_called_with(plugin, fn) self.assertEqual(2, call_plugin_serialized.call_count) self.assertEqual(2, retry_cb.call_count)
def test_serialized_with_retry_socket_reset_reraised(self): exc = socket.error() exc.errno = errno.ECONNRESET plugin = 'glance' fn = 'download_vhd' num_retries = 1 callback = None retry_cb = mock.Mock() with mock.patch.object(self.session, 'call_plugin_serialized', spec=True) as call_plugin_serialized: call_plugin_serialized.side_effect = exc self.assertRaises( exception.PluginRetriesExceeded, self.session.call_plugin_serialized_with_retry, plugin, fn, num_retries, callback, retry_cb) call_plugin_serialized.assert_called_with(plugin, fn) self.assertEqual(2, call_plugin_serialized.call_count)
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 read(self, n): buffer = [] while self.offset < self.file_size: try: data = self.stream.read(min(n, self.file_size - self.offset)) self.offset += len(data) n -= len(data) buffer.append(data) if n == 0 or data: break except socket.timeout: self._progress() self._restart() except socket.error as e: if e.errno != errno.ECONNRESET: raise self._progress() self._restart() return "".join(buffer)
def URL_to_HTML(URL): try: HTML = urllib2.urlopen(URL) except urllib2.HTTPError as error: print u'HTTPError: {0} ({1})'.format(URL, error.code) except urllib2.URLError as error: print u'URLError: {0} ({1})'.format(URL, error.reason) except httplib.BadStatusLine as error: print u'BadStatusLine: {}'.format(URL) except SocketError as error: if error.errno != errno.ECONNRESET: raise pass else: Charset = HTML.headers['content-type'][HTML.headers['content-type'].index('=') + 1:] HTML = unicode(HTML.read(), Charset) return HTML
def read(self, sz): try: buff = self.handle.recv(sz) except socket.error, e: if (e.args[0] == errno.ECONNRESET and (sys.platform == 'darwin' or sys.platform.startswith('freebsd'))): # freebsd and Mach don't follow POSIX semantic of recv # and fail with ECONNRESET if peer performed shutdown. # See corresponding comment and code in TSocket::read() # in lib/cpp/src/transport/TSocket.cpp. self.close() # Trigger the check to raise the END_OF_FILE exception below. buff = '' else: raise if len(buff) == 0: raise TTransportException(type=TTransportException.END_OF_FILE, message='TSocket read 0 bytes') return buff
def pcl_can_query_retry(e): """ ???????? ?????????? ??? ?????? pcl_query_retry ?? ??????????? ?????????? ??????? ?????????: e (Exception) -- ?????????? ?? pcl_query_retry ?????????: None ??? ?????????????? ?????????? """ if type(e) == pclError: # https://docs.pcloud.com/errors/index.html if e.errno >= 3000: pass # HTTP elif (e.errno >= 500 and e.errno < 600) or e.errno == 401 or e.errno == 429: pass else: raise e elif type(e) == socket.error and not (e.errno == errno.ECONNRESET or e.errno == errno.ECONNREFUSED): raise e
def sendfile_wrapper(self, sock, file, offset, nbytes, headers=[], trailers=[]): """A higher level wrapper representing how an application is supposed to use sendfile(). """ while 1: try: if self.SUPPORT_HEADERS_TRAILERS: return os.sendfile(sock, file, offset, nbytes, headers, trailers) else: return os.sendfile(sock, file, offset, nbytes) except OSError as err: if err.errno == errno.ECONNRESET: # disconnected raise elif err.errno in (errno.EAGAIN, errno.EBUSY): # we have to retry send data continue else: raise
def _write(self, length): # we need to write all the data but it's a non-blocking socket # so loop until it's all written eating EAGAIN exceptions data=self.netbuf_out_mv written=0 while written < length: try: written += self.sock_write(data[written:length]) except OSError as e: if len(e.args) > 0: if e.args[0] == errno.EAGAIN: # can't write yet, try again pass elif e.args[0] == errno.ECONNRESET: # connection closed return # we are done: TODO: error? else: raise else: # something else...propagate the exception raise
def _send_loop(self, send_method, data, *args): if self.act_non_blocking: return send_method(data, *args) while 1: try: return send_method(data, *args) except socket.error as e: eno = get_errno(e) if eno == errno.ENOTCONN or eno not in SOCKET_BLOCKING: raise try: self._trampoline(self.fd, write=True, timeout=self.gettimeout(), timeout_exc=socket.timeout("timed out")) except IOClosed: raise socket.error(errno.ECONNRESET, 'Connection closed by another thread')
def _handle_bt_error(self, bt_error): assert isinstance(bt_error, IOError) #'timed out' is caused by the wait_for_connection loop if isinstance(bt_error, socket.timeout): pass #'resource unavailable' is when data cannot be read because there is nothing in the buffer elif bt_error.errno == errno.EAGAIN: pass #'connection reset' is caused when the client disconnects elif bt_error.errno == errno.ECONNRESET: self._client_connected = False if self.when_client_disconnects: self.when_client_disconnects() #'conection timeout' is caused when the server can no longer connect to read from the client # (perhaps the client has gone out of range) elif bt_error.errno == errno.ETIMEDOUT: self._client_connected = False if self.when_client_disconnects: self.when_client_disconnects() else: raise bt_error
def do_GET(self): query = self.path.split("?",1)[-1] filepath = urllib.unquote_plus(query) self.suppress_socket_error_report = None self.send_headers(filepath) print "sending data" try: self.write_response(filepath) except socket.error, e: if isinstance(e.args, tuple): if e[0] in (errno.EPIPE, errno.ECONNRESET): print "disconnected" self.suppress_socket_error_report = True return raise
def test_listen(self): logger = self.useFixture(FakeLogger(format="%(asctime)s %(message)s")) self.executable.listen(6666) self.executable.sleep(1) self.executable.spawn() # Try to connect to the socket sock = socket.socket() transient = (errno.ECONNREFUSED, errno.ECONNRESET) attempts = 10 for i in range(1, attempts + 1): try: sock.connect(("127.0.0.1", self.executable.port)) except socket.error as error: # pragma: no cover if error.errno in transient and i != attempts: time.sleep(0.05 * i) continue logging.error("connection attempt %d failed", i) raise error break self.assertEqual("127.0.0.1", sock.getsockname()[0])
def _on_local_error(self): if self._local_sock: err = eventloop.get_sock_error(self._local_sock) if err.errno not in [errno.ECONNRESET, errno.EPIPE]: logging.error(err) logging.error("local error, exception from %s:%d" % (self._client_address[0], self._client_address[1])) self.destroy()
def _on_remote_error(self): if self._remote_sock: err = eventloop.get_sock_error(self._remote_sock) if err.errno not in [errno.ECONNRESET]: logging.error(err) if self._remote_address: logging.error("remote error, when connect to %s:%d" % (self._remote_address[0], self._remote_address[1])) else: logging.error("remote error, exception from %s:%d" % (self._client_address[0], self._client_address[1])) self.destroy()
def _read_to_buffer(self): """Reads from the socket and appends the result to the read buffer. Returns the number of bytes read. Returns 0 if there is nothing to read (i.e. the read returns EWOULDBLOCK or equivalent). On error closes the socket and raises an exception. """ while True: try: chunk = self.read_from_fd() except (socket.error, IOError, OSError) as e: if errno_from_exception(e) == errno.EINTR: continue # ssl.SSLError is a subclass of socket.error if self._is_connreset(e): # Treat ECONNRESET as a connection close rather than # an error to minimize log spam (the exception will # be available on self.error for apps that care). self.close(exc_info=True) return self.close(exc_info=True) raise break if chunk is None: return 0 self._read_buffer.append(chunk) self._read_buffer_size += len(chunk) if self._read_buffer_size > self.max_buffer_size: gen_log.error("Reached maximum read buffer size") self.close() raise StreamBufferFullError("Reached maximum read buffer size") return len(chunk)
def _is_connreset(self, exc): """Return true if exc is ECONNRESET or equivalent. May be overridden in subclasses. """ return (isinstance(exc, (socket.error, IOError)) and errno_from_exception(exc) in _ERRNO_CONNRESET)
def write(self, obj): try: size = self.socket.send(obj) if size == 0: raise ConnectionClosed() return size except socket.error, e: if e.errno == errno.EAGAIN: raise ConnectionAgain(str(e)) if e.errno == errno.EPIPE: raise ConnectionClosed(str(e)) if e.errno == errno.ECONNRESET: raise ConnectionClosed(str(e)) raise
def read(self, size): try: tmp = self.socket.recv(size) if not tmp: raise ConnectionClosed() return tmp except socket.error, e: if e.errno == errno.EAGAIN: raise ConnectionAgain(str(e)) if e.errno == errno.EBADF: raise ConnectionClosed(str(e)) if e.errno == errno.ECONNRESET: raise ConnectionClosed(str(e)) raise
def _recvall_blocking(conn, amount): data = [] while amount > 0: with wrapped_socket_errnos(errno.ECONNRESET): piece = conn.recv(amount) if not piece: raise _ConnectionLost("could not recv() all bytes") data.append(piece) amount -= len(piece) return "".join(data)
def send_encoded(conn, obj): msg_bytes = cPickle.dumps(obj, cPickle.HIGHEST_PROTOCOL) data = struct.pack("!I", len(msg_bytes)) + msg_bytes with wrapped_socket_errnos(errno.ECONNRESET, errno.EPIPE): conn.sendall(data)
def _recvall_stream(sock, amount, timeout=None): data = [] while amount > 0: with wrapped_socket_errnos(errno.ECONNRESET): piece = yield sock.recv(amount, timeout=timeout) if not piece: raise _ConnectionLost("could not recv() all bytes") data.append(piece) amount -= len(piece) idiokit.stop("".join(data))
def io_op(func, *args): try: return func(*args), False except OSError, e: IOLOG.debug('io_op(%r) -> OSError: %s', func, e) if e.errno not in (errno.EIO, errno.ECONNRESET, errno.EPIPE): raise return None, True
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 _send_cb(self, frame, tolerate_peer_gone=False): """ This is the callback used by streams to send data on the connection. It expects to receive a single frame, and then to serialize that frame and send it on the connection. It does so obeying the connection-level flow-control principles of HTTP/2. """ # Maintain our outgoing flow-control window. if frame.type == DataFrame.type: # If we don't have room in the flow control window, we need to look # for a Window Update frame. while self._out_flow_control_window < len(frame.data): self._recv_cb() self._out_flow_control_window -= len(frame.data) data = frame.serialize() max_frame_size = self._settings[SettingsFrame.SETTINGS_MAX_FRAME_SIZE] if frame.body_len > max_frame_size: raise ValueError( "Frame size %d exceeds maximum frame size setting %d" % (frame.body_len, self._settings[SettingsFrame.SETTINGS_MAX_FRAME_SIZE]) ) log.info( "Sending frame %s on stream %d", frame.__class__.__name__, frame.stream_id ) try: self._sock.send(data) except socket.error as e: if (not tolerate_peer_gone or e.errno not in (errno.EPIPE, errno.ECONNRESET)): raise
def send_loop(self): while connect_control.keep_running and self.keep_running: frame = self.send_queue.get(True) if not frame: # None frame to exist break # xlog.debug("%s Send:%s", self.ip, str(frame)) data = frame.serialize() try: self._sock.send(data, flush=False) # don't flush for small package # reduce send api call # wait for payload frame time.sleep(0.001) # combine header and payload in one tcp package. if not self.send_queue._qsize(): self._sock.flush() except socket.error as e: if e.errno not in (errno.EPIPE, errno.ECONNRESET): xlog.warn("%s http2 send fail:%r", self.ip, e) else: xlog.exceptiong("send error:%r", e) self.close("send fail:%r", e)
def handle(self, listener, client, addr): req = None try: if self.cfg.is_ssl: client = ssl.wrap_socket(client, server_side=True, **self.cfg.ssl_options) parser = http.RequestParser(self.cfg, client) req = six.next(parser) self.handle_request(listener, req, client, addr) except http.errors.NoMoreData as e: self.log.debug("Ignored premature client disconnection. %s", e) except StopIteration as e: self.log.debug("Closing connection. %s", e) except ssl.SSLError as e: if e.args[0] == ssl.SSL_ERROR_EOF: self.log.debug("ssl connection closed") client.close() else: self.log.debug("Error processing SSL request.") self.handle_error(req, client, addr, e) except socket.error as e: if e.args[0] not in (errno.EPIPE, errno.ECONNRESET): self.log.exception("Socket error processing request.") else: if e.args[0] == errno.ECONNRESET: self.log.debug("Ignoring connection reset") else: self.log.debug("Ignoring EPIPE") except Exception as e: self.handle_error(req, client, addr, e) finally: util.close(client)