我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用socket.SO_ERROR。
def _handle_connect(self): err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: self.error = socket.error(err, os.strerror(err)) # IOLoop implementations may vary: some of them return # an error state before the socket becomes writable, so # in that case a connection failure would be handled by the # error path in _handle_events instead of here. if self._connect_future is None: gen_log.warning("Connect error on fd %s: %s", self.socket.fileno(), errno.errorcode[err]) self.close() return if self._connect_callback is not None: callback = self._connect_callback self._connect_callback = None self._run_callback(callback) if self._connect_future is not None: future = self._connect_future self._connect_future = None future.set_result(self) self._connecting = False
def _sock_connect_cb(self, fut, sock, address): if fut.cancelled(): return try: err = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: # Jump to any except clause below. raise OSError(err, 'Connect call failed %s' % (address,)) except (BlockingIOError, InterruptedError): # socket is still registered, the callback will be retried later pass except Exception as exc: fut.set_exception(exc) else: fut.set_result(None)
def handle_connect_event(self): if not self.handshaking: err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: raise socket.error(err, _strerror(err)) self.socket = ssl.wrap_socket (self.socket, do_handshake_on_connect = False) self.handshaking = True try: self.socket.do_handshake () except ssl.SSLError as why: if why.args[0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE): return # retry handshake raise ssl.SSLError(why) # handshaking done self.handle_connect() self.connected = True
def _handle_connect(self): err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: # IOLoop implementations may vary: some of them return # an error state before the socket becomes writable, so # in that case a connection failure would be handled by the # error path in _handle_events instead of here. logging.warning("Connect error on fd %d: %s", self.socket.fileno(), errno.errorcode[err]) self.close() return if self._connect_callback is not None: callback = self._connect_callback self._connect_callback = None self._run_callback(callback) self._connecting = False
def _handle_connect(self): err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: self.error = socket.error(err, os.strerror(err)) # IOLoop implementations may vary: some of them return # an error state before the socket becomes writable, so # in that case a connection failure would be handled by the # error path in _handle_events instead of here. gen_log.warning("Connect error on fd %d: %s", self.socket.fileno(), errno.errorcode[err]) self.close() return if self._connect_callback is not None: callback = self._connect_callback self._connect_callback = None self._run_callback(callback) self._connecting = False
def _Dynamic_GetSocketOptions(self, request, response): state = self._LookupSocket(request.socket_descriptor()) for opt in request.options_list(): if (opt.level() == remote_socket_service_pb.SocketOption.SOCKET_SOL_SOCKET and opt.option() == remote_socket_service_pb.SocketOption.SOCKET_SO_ERROR): ret = response.add_options() ret.set_level(opt.level()) ret.set_option(opt.option()) ret.set_value( state.sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR, 1024)) else: value = self._mock_options.GetMockValue(opt.level(), opt.option()) if value is None: raise apiproxy_errors.ApplicationError( RemoteSocketServiceError.PERMISSION_DENIED, 'Attempt to get blocked socket option.') ret = response.add_options() ret.set_level(opt.level()) ret.set_option(opt.option()) ret.set_value(value)
def handle_connect(self): """Called when connection is established.""" self.del_channel() if self._idler is not None and not self._idler.cancelled: self._idler.cancel() if not self.cmd_channel.connected: return self.close() # fix for asyncore on python < 2.6, meaning we aren't # actually connected. # test_active_conn_error tests this condition err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: raise socket.error(err) # msg = 'Active data connection established.' self.cmd_channel.respond('200 ' + msg) self.cmd_channel.log_cmd(self._cmd, self._normalized_addr, 200, msg) # if not self.cmd_channel.connected: return self.close() # delegate such connection to DTP handler handler = self.cmd_channel.dtp_handler(self.socket, self.cmd_channel) self.cmd_channel.data_channel = handler self.cmd_channel._on_dtp_connection()
def _connect(self, sock, sa): while not self._canceled and not ABORT_FLAG_FUNCTION(): time.sleep(0.01) self._check_timeout() # this should be done at the beginning of each loop status = sock.connect_ex(sa) if not status or status in (errno.EISCONN, WIN_EISCONN): break elif status in (errno.EINPROGRESS, WIN_EWOULDBLOCK): self.deadline = time.time() + self._timeout.getConnectTimeout() # elif status in (errno.EWOULDBLOCK, errno.EALREADY) or (os.name == 'nt' and status == errno.WSAEINVAL): # pass yield if self._canceled or ABORT_FLAG_FUNCTION(): raise CanceledException('Request canceled') error = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if error: # TODO: determine when this case can actually happen raise socket.error((error,))
def handle_connect_event(self): err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: raise socket.error(err, _strerror(err)) self.handle_connect() self.connected = True
def handle_write_event(self): if self.accepting: # Accepting sockets shouldn't get a write event. # We will pretend it didn't happen. return if not self.connected: #check for errors err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: raise socket.error(err, _strerror(err)) self.handle_connect_event() self.handle_write()
def handle_expt_event(self): # handle_expt_event() is called if there might be an error on the # socket, or if there is OOB data # check for the error condition first err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: # we can get here when select.select() says that there is an # exceptional condition on the socket # since there is an error, we'll go ahead and close the socket # like we would in a subclassed handle_read() that received no # data self.handle_close() else: self.handle_expt()
def getsockopt(self, level, optname, buflen=None): if (level == socket.SOL_SOCKET and optname == socket.SO_ERROR and not buflen): return 0 raise NotImplementedError("Only asyncore specific behaviour " "implemented.")
def getsockopt(self, level, optname, buflen=None): # act like an unerrored socket for the asyncore error handling if level == socket.SOL_SOCKET and optname == socket.SO_ERROR and not buflen: return 0 raise NotImplementedError()
def get_sock_error(sock): error_number = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) return socket.error(error_number, os.strerror(error_number))
def get_fd_error(self): errno = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) return socket.error(errno, os.strerror(errno))
def connect(self, timeout=None, optimist=False): if timeout != None: limit = time.time() + timeout while True: if timeout != None and time.time() > limit: raise ConnectionTimeout('connection attempt timed out') try: Connection.connect(self) except ConnectionInProgress: if timeout == None: events = self.poll(select.POLLOUT, -1) else: events = self.poll(select.POLLOUT, timeout) if not events: raise ConnectionTimeout('connection attempt timed out') if events[0][1] & (select.POLLERR | select.POLLHUP): if optimist: time.sleep(0.1) continue raise ConnectionRefused() if events[0][1] & select.POLLOUT: e = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if e == errno.ECONNREFUSED: raise ConnectionRefused() return return # good
def handle_connect_event(self): err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: raise socket.error(err, _strerror(err)) self.handle_connect() self.connected = True self.connecting = False
def handle_expt_event (self): err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: self.handle_close (703, "Socket %d Error" % err) else: self.handle_expt ()
def handshake (self): if not self._handshaking: err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) if err != 0: raise OSError(err, asyncore._strerror(err)) ssl_context = create_urllib3_context(ssl_version=resolve_ssl_version(None), cert_reqs=resolve_cert_reqs(None)) if self.ac_negotiate_http2: try: ssl_context.set_alpn_protocols (H2_PROTOCOLS) except AttributeError: ssl_context.set_npn_protocols (H2_PROTOCOLS) self.socket = ssl_context.wrap_socket (self.socket, do_handshake_on_connect = False, server_hostname = self.address [0]) self._handshaking = True try: self.socket.do_handshake () except ssl.SSLError as why: if why.args [0] in (ssl.SSL_ERROR_WANT_READ, ssl.SSL_ERROR_WANT_WRITE): return False raise ssl.SSLError(why) try: self._proto = self.socket.selected_alpn_protocol() except (AttributeError, NotImplementedError): try: self._proto = self.socket.selected_npn_protocol() except (AttributeError, NotImplementedError): pass self._handshaked = True return True
def read(self): try: incoming = self.__socket.recv(2 ** 13) except socket.error as e: if e.errno != socket.errno.EAGAIN: self.__disconnected = True return False if self.__socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR): self.__disconnected = True return False if not incoming: return False self.__lastReadTime = time.time() self.__readBuffer += incoming return True
def __processConnection(self, descr, eventType): if descr != self.__conn.fileno(): self.__poller.unsubscribe(descr) return isError = False if eventType & POLL_EVENT_TYPE.ERROR: isError = True if eventType & POLL_EVENT_TYPE.READ or eventType & POLL_EVENT_TYPE.WRITE: if self.__conn.socket().getsockopt(socket.SOL_SOCKET, socket.SO_ERROR): isError = True else: if self.__status == NODE_STATUS.CONNECTING: self.__conn.send(self.__syncObj()._getSelfNodeAddr()) self.__status = NODE_STATUS.CONNECTED if isError or self.__conn.isDisconnected(): self.__status = NODE_STATUS.DISCONNECTED self.__conn.close() self.__poller.unsubscribe(descr) return if eventType & POLL_EVENT_TYPE.WRITE: if self.__status == NODE_STATUS.CONNECTING: self.__conn.send(self.__syncObj()._getSelfNodeAddr()) self.__status = NODE_STATUS.CONNECTED self.__conn.trySendBuffer() event = POLL_EVENT_TYPE.READ | POLL_EVENT_TYPE.ERROR if self.__conn.getSendBufferSize() > 0: event |= POLL_EVENT_TYPE.WRITE if not self.__conn.isDisconnected(): self.__poller.subscribe(descr, self.__processConnection, event) if eventType & POLL_EVENT_TYPE.READ: if self.__conn.read(): while True: message = self.__conn.getMessage() if message is None: break self.__syncObj()._onMessageReceived(self.__nodeAddr, message)