Python selectors.DefaultSelector() Examples

The following are 30 code examples of selectors.DefaultSelector(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module selectors , or try the search function .
Example #1
Source File: server.py    From mock-ssh-server with MIT License 8 votes vote down vote up
def _run(self):
        sock = self._socket
        selector = selectors.DefaultSelector()
        selector.register(sock, selectors.EVENT_READ)
        while sock.fileno() > 0:
            self.log.debug("Waiting for incoming connections ...")
            events = selector.select(timeout=1.0)
            if events:
                try:
                    conn, addr = sock.accept()
                except OSError as ex:
                    if ex.errno in (errno.EBADF, errno.EINVAL):
                        break
                    raise
                self.log.debug("... got connection %s from %s", conn, addr)
                handler = Handler(self, (conn, addr))
                t = threading.Thread(target=handler.run)
                t.setDaemon(True)
                t.start() 
Example #2
Source File: tcp_client.py    From LiMEaide with GNU General Public License v3.0 7 votes vote down vote up
def run(self):
        retry = True

        self.logger.info("Connecting to Socket")
        sel = selectors.DefaultSelector()
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        conn.setsockopt(
            socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
        conn.setblocking(False)

        conn.connect_ex((self.ip, self.port))
        sel.register(conn, selectors.EVENT_READ, data=None)

        while retry:
            events = sel.select()
            for key, mask in events:
                retry = self.__handle_client__(key, mask, sel)

        sel.unregister(conn)
        if self.result['success']:
            conn.shutdown(socket.SHUT_RDWR)

        self.qresult.put(self.result)
        self.logger.info("Socket Closed") 
Example #3
Source File: common_func.py    From shootback with MIT License 6 votes vote down vote up
def select_recv(conn, buff_size, timeout=None):
    """add timeout for socket.recv()
    :type conn: socket.socket
    :type buff_size: int
    :type timeout: float
    :rtype: Union[bytes, None]
    """
    if selectors:
        sel = selectors.DefaultSelector()
        sel.register(conn, EVENT_READ)
        events = sel.select(timeout)
        sel.close()
        if not events:
            # timeout
            raise RuntimeError("recv timeout")
    else:
        rlist, _, _ = select.select([conn], [], [], timeout)

    buff = conn.recv(buff_size)
    if not buff:
        raise RuntimeError("received zero bytes, socket was closed")

    return buff 
Example #4
Source File: _wait.py    From sofa-bolt-python with Apache License 2.0 6 votes vote down vote up
def _wait_for_io_events(socks, events, timeout):
    """ Waits for IO events to be available from a list of sockets
    or optionally a single socket if passed in. Returns a list of
    sockets that can be interacted with immediately. """
    if not isinstance(socks, list):
        # Probably just a single socket.
        if hasattr(socks, "fileno"):
            socks = [socks]
        # Otherwise it might be a non-list iterable.
        else:
            socks = list(socks)
    with DefaultSelector() as selector:
        for sock in socks:
            try:
                selector.register(sock, events)
            except Exception as e:
                raise SocketValueError("SocketValueError", e)
        return [key[0].fileobj for key in
                selector.select(timeout) if key[1] & events] 
Example #5
Source File: network_manager.py    From futuquant with Apache License 2.0 5 votes vote down vote up
def _create_all(self):
        self._selector = selectors.DefaultSelector()
        self._next_conn_id = 1
        self._req_queue = queue.Queue()
        self._sync_req_timeout = 12
        self._thread = None
        now = datetime.now()
        self._last_activate_time = now
        self._last_check_req_time = now
        self._r_sock, self._w_sock = make_ctrl_socks()
        self._selector.register(self._r_sock, selectors.EVENT_READ) 
Example #6
Source File: selector_events.py    From android_universal with MIT License 5 votes vote down vote up
def __init__(self, selector=None):
        super().__init__()

        if selector is None:
            selector = selectors.DefaultSelector()
        logger.debug('Using selector: %s', selector.__class__.__name__)
        self._selector = selector
        self._make_self_pipe()
        self._transports = weakref.WeakValueDictionary() 
Example #7
Source File: network_manager.py    From py-futu-api with Apache License 2.0 5 votes vote down vote up
def _create_all(self):
        self._selector = selectors.DefaultSelector()
        self._req_queue = queue.Queue()
        self._sync_req_timeout = 12
        self._thread = None
        now = datetime.now()
        self._last_activate_time = now
        self._last_check_req_time = now
        self._r_sock, self._w_sock = make_ctrl_socks()
        self._selector.register(self._r_sock, selectors.EVENT_READ) 
Example #8
Source File: selector_events.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, selector=None):
        super().__init__()

        if selector is None:
            selector = selectors.DefaultSelector()
        logger.debug('Using selector: %s', selector.__class__.__name__)
        self._selector = selector
        self._make_self_pipe()
        self._transports = weakref.WeakValueDictionary() 
Example #9
Source File: testserver.py    From deckard with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def query_io(self):
        """ Main server process """
        self.undefined_answers = 0
        with self.active_lock:
            if not self.active:
                raise Exception("[query_io] Test server not active")
        while True:
            with self.condition:
                self.condition.notify()
            with self.active_lock:
                if not self.active:
                    break
            objects = self.srv_socks + self.connections
            sel = selectors.DefaultSelector()
            for obj in objects:
                sel.register(obj, selectors.EVENT_READ)
            items = sel.select(0.1)
            for key, event in items:
                sock = key.fileobj
                if event & selectors.EVENT_READ:
                    if sock in self.srv_socks:
                        if sock.proto == socket.IPPROTO_TCP:
                            conn, _ = sock.accept()
                            self.connections.append(conn)
                        else:
                            self.handle_query(sock)
                    elif sock in self.connections:
                        if not self.handle_query(sock):
                            sock.close()
                            self.connections.remove(sock)
                    else:
                        raise Exception(
                            "[query_io] Socket IO internal error {}, exit"
                            .format(sock.getsockname()))
                else:
                    raise Exception("[query_io] Socket IO error {}, exit"
                                    .format(sock.getsockname())) 
Example #10
Source File: testserver.py    From deckard with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def query_io(self):
        """ Main server process """
        self.undefined_answers = 0
        with self.active_lock:
            if not self.active:
                raise Exception("[query_io] Test server not active")
        while True:
            with self.condition:
                self.condition.notify()
            with self.active_lock:
                if not self.active:
                    break
            objects = self.srv_socks + self.connections
            sel = selectors.DefaultSelector()
            for obj in objects:
                sel.register(obj, selectors.EVENT_READ)
            items = sel.select(0.1)
            for key, event in items:
                sock = key.fileobj
                if event & selectors.EVENT_READ:
                    if sock in self.srv_socks:
                        if sock.proto == socket.IPPROTO_TCP:
                            conn, _ = sock.accept()
                            self.connections.append(conn)
                        else:
                            self.handle_query(sock)
                    elif sock in self.connections:
                        if not self.handle_query(sock):
                            sock.close()
                            self.connections.remove(sock)
                    else:
                        raise Exception(
                            "[query_io] Socket IO internal error {}, exit"
                            .format(sock.getsockname()))
                else:
                    raise Exception("[query_io] Socket IO error {}, exit"
                                    .format(sock.getsockname())) 
Example #11
Source File: selector_events.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, selector=None):
        super().__init__()

        if selector is None:
            selector = selectors.DefaultSelector()
        logger.debug('Using selector: %s', selector.__class__.__name__)
        self._selector = selector
        self._make_self_pipe()
        self._transports = weakref.WeakValueDictionary() 
Example #12
Source File: common_func.py    From shootback with MIT License 5 votes vote down vote up
def __init__(self):
        self.conn_rd = set()  # record readable-sockets
        self.conn_wr = set()  # record writeable-sockets
        self.map = {}  # record sockets pairs
        self.callbacks = {}  # record callbacks
        self.send_buff = {}  # buff one packet for those sending too-fast socket

        if selectors:
            self.sel = selectors.DefaultSelector()
        else:
            self.sel = None 
Example #13
Source File: eth.py    From pyxcp with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, config=None):
        super(Eth, self).__init__(config)
        self.loadConfig(config)
        self.host = self.config.get("HOST")
        self.port = self.config.get("PORT")
        self.protocol = self.config.get("PROTOCOL")
        self.ipv6 = self.config.get("IPV6")
        self.use_tcp_no_delay = self.config.get("TCP_NODELAY")

        if self.ipv6 and not socket.has_ipv6:
            raise RuntimeError("IPv6 not supported by your platform.")
        else:
           address_family = socket.AF_INET6 if self.ipv6 else socket.AF_INET
        proto = socket.SOCK_STREAM if self.protocol == 'TCP' else socket.SOCK_DGRAM
        if self.host.lower() == "localhost":
            self.host = "::1" if self.ipv6 else "localhost"
        addrinfo = socket.getaddrinfo(self.host, self.port, address_family, proto)
        (self.address_family, self.socktype, self.proto, self.canonname, self.sockaddr) = addrinfo[0]
        self.status = 0
        self.sock = socket.socket(
            self.address_family,
            self.socktype,
            self.proto
        )
        self.selector = selectors.DefaultSelector()
        self.selector.register(self.sock, selectors.EVENT_READ)
        self.use_tcp = (self.protocol == 'TCP')
        self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        if self.use_tcp and self.use_tcp_no_delay:
            self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        if hasattr(socket, "SO_REUSEPORT"):
            self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        self.sock.settimeout(0.5) 
Example #14
Source File: WfModule.py    From cjworkbench with GNU Affero General Public License v3.0 5 votes vote down vote up
def _await_stdout_ready(self):
        """
        Wait until `self.process.stdout.read()` is guaranteed not to block.
        """
        with selectors.DefaultSelector() as selector:
            selector.register(self.process.stdout, selectors.EVENT_READ)
            while len(selector.select()) == 0:
                pass 
Example #15
Source File: thread_tcp_server.py    From feeluown-core with MIT License 5 votes vote down vote up
def run(self, *args, **kwargs):
        # TODO: enable TCP_QUICKACK?
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self.host, self.port))
        sock.listen()
        logger.debug('Tcp server running at %s:%d' % (self.host, self.port))
        self._sock = sock
        # TODO: choose a better selector?
        with selectors.DefaultSelector() as sel:
            # NOTE: use selector timeout and server status flag(threading.Event)
            # to make tcp server stoppable
            sel.register(self._sock, selectors.EVENT_READ)
            while not self._close_event.is_set():
                # TODO: better default interval settings?
                ready = sel.select(0.5)
                if not ready:
                    continue
                try:
                    conn, addr = sock.accept()
                except OSError:  # sock is closed
                    logger.debug('Tcp server is closed.')
                    break
                except ConnectionError as e:
                    logger.warning(e)
                    break
                else:
                    logger.debug('%s:%d connected.' % addr)
                    Thread(target=self.handle_func, args=(conn, addr, *args),
                           kwargs=kwargs, name='TcpClientThread').start()
            logger.debug('Tcp server is stopped.') 
Example #16
Source File: inputhook.py    From python-prompt-toolkit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def new_eventloop_with_inputhook(
    inputhook: Callable[["InputHookContext"], None]
) -> AbstractEventLoop:
    """
    Create a new event loop with the given inputhook.
    """
    selector = InputHookSelector(selectors.DefaultSelector(), inputhook)
    loop = asyncio.SelectorEventLoop(selector)
    return loop 
Example #17
Source File: sftpd.py    From geofront with GNU Affero General Public License v3.0 5 votes vote down vote up
def start_server(path: str, host: str, port: int, terminated: threading.Event):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.settimeout(1)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    server_socket.bind((host, port))
    server_socket.listen(1)
    stub_cls = type('StubSFTPServer', (StubSFTPServer,), {'ROOT': path})
    host_key = RSAKey.generate(1024)

    def accept(server_socket, mask):
        conn, addr = server_socket.accept()
        transport = Transport(conn)
        transport.add_server_key(host_key)
        transport.set_subsystem_handler('sftp', SFTPServer, stub_cls)
        server = StubServer(path)
        transport.start_server(server=server)
        while not terminated.is_set():
            channel = transport.accept(1)
            if channel is not None and not terminated.is_set():
                while transport.is_active() and not terminated.is_set():
                    terminated.wait(1)
                break

    sel = selectors.DefaultSelector()
    sel.register(server_socket, selectors.EVENT_READ, accept)
    last_used = time.time()
    while not terminated.is_set() and last_used + 10 > time.time():
        events = sel.select(1)
        for key, mask in events:
            key.data(key.fileobj, mask)
            last_used = time.time() 
Example #18
Source File: circuit.py    From torpy with Apache License 2.0 5 votes vote down vote up
def __init__(self, tor_socket, handler_mgr):
        super().__init__(name='RecvLoop_{}'.format(tor_socket.ip_address[0:7]))

        self._tor_socket = tor_socket

        self._handler_mgr = handler_mgr
        self._do_loop = False

        # fmt: off
        self._regs_funcs_map = {
            'reg': {
                socket.socket: self.register_socket,
                TorStream: self.register_stream
            },
            'unreg': {
                socket.socket: self.unregister_socket,
                TorStream: self.unregister_stream
            }
        }
        # fmt: on
        self._stream_to_callback = {}
        self._selector = DefaultSelector()

        self._cntrl_r, self._cntrl_w = socket.socketpair()
        self._selector.register(self._cntrl_r, EVENT_READ, self._do_stop)
        self._selector.register(self._tor_socket.ssl_socket, EVENT_READ, self._do_recv) 
Example #19
Source File: stream.py    From torpy with Apache License 2.0 5 votes vote down vote up
def __init__(self, our_sock, send_func):
        super().__init__(name='SocketLoop{:x}'.format(our_sock.fileno()))
        self._our_sock = our_sock
        self._send_func = send_func
        self._do_loop = True

        self._cntrl_r, self._cntrl_w = socket.socketpair()

        self._selector = DefaultSelector()
        self._selector.register(self._our_sock, EVENT_READ, self._do_recv)
        self._selector.register(self._cntrl_r, EVENT_READ, self._do_stop) 
Example #20
Source File: socket_client_connector.py    From clai with MIT License 5 votes vote down vote up
def __init__(self, host: str, port: int):
        self.sel = selectors.DefaultSelector()
        self.host = host
        self.port = port
        self.uuid = uuid.uuid4() 
Example #21
Source File: socket_server_connector.py    From clai with MIT License 5 votes vote down vote up
def __init__(self, server_status_datasource: ServerStatusDatasource):
        self.server_status_datasource = server_status_datasource
        self.sel = selectors.DefaultSelector()
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
Example #22
Source File: thread_tcp_server.py    From FeelUOwn with GNU General Public License v3.0 5 votes vote down vote up
def run(self, *args, **kwargs):
        # TODO: enable TCP_QUICKACK?
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self.host, self.port))
        sock.listen()
        logger.debug('Tcp server running at %s:%d' % (self.host, self.port))
        self._sock = sock
        # TODO: choose a better selector?
        with selectors.DefaultSelector() as sel:
            # NOTE: use selector timeout and server status flag(threading.Event)
            # to make tcp server stoppable
            sel.register(self._sock, selectors.EVENT_READ)
            while not self._close_event.is_set():
                # TODO: better default interval settings?
                ready = sel.select(0.5)
                if not ready:
                    continue
                try:
                    conn, addr = sock.accept()
                except OSError:  # sock is closed
                    logger.debug('Tcp server is closed.')
                    break
                except ConnectionError as e:
                    logger.warning(e)
                    break
                else:
                    logger.debug('%s:%d connected.' % addr)
                    Thread(target=self.handle_func, args=(conn, addr, *args),
                           kwargs=kwargs, name='TcpClientThread').start()
            logger.debug('Tcp server is stopped.') 
Example #23
Source File: _redirection.py    From imatlab with MIT License 5 votes vote down vote up
def redirect(fd, callback):
    save_fd = os.dup(fd)

    s_in, s_out = socket.socketpair()
    os.dup2(s_in.fileno(), fd)
    os.close(s_in.fileno())
    s_out.setblocking(False)

    sel = DefaultSelector()
    sel.register(s_out, EVENT_READ)

    def target():
        while running:
            _, = sel.select()  # There's only one event.
            callback(s_out.recv(4096))

    running = True
    thread = Thread(target=target, daemon=True)
    thread.start()

    try:
        yield
    finally:
        running = False
        os.dup2(save_fd, fd)
        os.close(save_fd) 
Example #24
Source File: test_android.py    From chaquopy with MIT License 5 votes vote down vote up
def test_select(self):
        import select
        self.assertFalse(hasattr(select, "kevent"))
        self.assertFalse(hasattr(select, "kqueue"))

        import selectors
        self.assertIs(selectors.DefaultSelector, selectors.EpollSelector) 
Example #25
Source File: selector_events.py    From Imogen with MIT License 5 votes vote down vote up
def __init__(self, selector=None):
        super().__init__()

        if selector is None:
            selector = selectors.DefaultSelector()
        logger.debug('Using selector: %s', selector.__class__.__name__)
        self._selector = selector
        self._make_self_pipe()
        self._transports = weakref.WeakValueDictionary() 
Example #26
Source File: utils.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def poll_streams(*files, line_timeout=None, global_timeout=None):
    """
    Poll a set of file objects for new data and return it line by line.

    The file objects should be line-buffered or unbuffered.  Regular files won't
    work on some systems (notably Linux, where DefaultSelector uses epoll() by
    default; this function is pointless for regular files anyway, since they are
    always ready for reading and writing).

    Each line returned is a 2-items tuple, with the first item being the object
    implementing the file interface, and the second the text read.

    The optional parameters line_timeout and global_timeout specify how long in
    seconds to wait at most for a single line or for all lines.  If no timeout
    is specified, this function will block indefintely for each line.
    """
    selector = selectors.DefaultSelector()
    for fileobj in files:
        selector.register(fileobj, selectors.EVENT_READ)
    timeout = _line_timeout_generator(line_timeout, global_timeout)
    while selector.get_map():
        try:
            next_timeout = next(timeout)
        except StopIteration:
            logger.error('_line_timeout_generator returned (should never happen).'
                         'line_timeout: %s, global_timeout: %s',
                         line_timeout, global_timeout)
            next_timeout = 0
        available = selector.select(next_timeout)
        if not available:
            # TODO(smarnach): This can also mean that the process received a signal.
            raise TimeoutError('Could not read line before timeout: {timeout}'.format(timeout=timeout))
        for key, unused_mask in available:
            line = key.fileobj.readline()
            if line:
                yield (key.fileobj, line)
            else:
                selector.unregister(key.fileobj) 
Example #27
Source File: svr_threads.py    From Pyro5 with MIT License 5 votes vote down vote up
def __init__(self):
        self.daemon = self.sock = self._socketaddr = self.locationStr = self.pool = None
        self.shutting_down = False
        self.housekeeper = None
        self._selector = selectors.DefaultSelector() 
Example #28
Source File: svr_multiplex.py    From Pyro5 with MIT License 5 votes vote down vote up
def __init__(self):
        self.sock = self.daemon = self.locationStr = None
        self.selector = selectors.DefaultSelector()
        self.shutting_down = False 
Example #29
Source File: client.py    From sofa-bolt-python with Apache License 2.0 4 votes vote down vote up
def invoke_sync(self, interface, method_name, content, spanctx=None, target_app="", uid="", timeout_ms=None):
        """
        :param request:
        :param timeout: if timeout > 0, this specifies the maximum wait time, in
                   seconds
                   if timeout <= 0, the select() call won't block, and will
                   report the currently ready file objects
                   if timeout is None, select() will block until a monitored
                   file object becomes ready
        :return: serialized response content
        :raise: TimeoutError
        """
        assert isinstance(timeout_ms, (int, float))
        pkg = BoltResponse
        deadline = time.time() + timeout_ms / 1000 + 1
        req_id, c = self._raw_invoke(interface, method_name, content, target_app=target_app, uid=uid,
                                     spanctx=spanctx, timeout_ms=timeout_ms)

        with DefaultSelector() as sel:
            sel.register(c, EVENT_READ)
            total_size = pkg.bolt_header_size()
            resp_bytes = b''
            header = None
            while len(resp_bytes) < total_size:
                ready = sel.select(timeout=deadline - time.time())
                if not ready:
                    # timeout
                    c.close()
                    raise TimeoutError('Sync call timeout')
                for key, event in ready:
                    resp_bytes += key.fileobj.recv(total_size - len(resp_bytes))
                if not header and len(resp_bytes) >= total_size:
                    header = pkg.bolt_header_from_stream(resp_bytes)
                    body_size = header['class_len'] + header['header_len'] + header['content_len']
                    total_size += body_size
        self._get_pool(interface).put_conn(c)

        resp = pkg.bolt_content_from_stream(resp_bytes[pkg.bolt_header_size():], header)
        if resp.request_id != req_id:
            raise ServerError("RequestId not match")
        if resp.respstatus != RESPSTATUS.SUCCESS:
            raise ServerError.from_statuscode(resp.respstatus)
        return resp.content 
Example #30
Source File: forkserver.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 4 votes vote down vote up
def main(listener_fd, alive_r, preload, main_path=None, sys_path=None):
    '''Run forkserver.'''
    if preload:
        if '__main__' in preload and main_path is not None:
            process.current_process()._inheriting = True
            try:
                spawn.import_main_path(main_path)
            finally:
                del process.current_process()._inheriting
        for modname in preload:
            try:
                __import__(modname)
            except ImportError:
                pass

    util._close_stdin()

    # ignoring SIGCHLD means no need to reap zombie processes;
    # letting SIGINT through avoids KeyboardInterrupt tracebacks
    handlers = {
        signal.SIGCHLD: signal.SIG_IGN,
        signal.SIGINT: signal.SIG_DFL,
        }
    old_handlers = {sig: signal.signal(sig, val)
                    for (sig, val) in handlers.items()}

    with socket.socket(socket.AF_UNIX, fileno=listener_fd) as listener, \
         selectors.DefaultSelector() as selector:
        _forkserver._forkserver_address = listener.getsockname()

        selector.register(listener, selectors.EVENT_READ)
        selector.register(alive_r, selectors.EVENT_READ)

        while True:
            try:
                while True:
                    rfds = [key.fileobj for (key, events) in selector.select()]
                    if rfds:
                        break

                if alive_r in rfds:
                    # EOF because no more client processes left
                    assert os.read(alive_r, 1) == b''
                    raise SystemExit

                assert listener in rfds
                with listener.accept()[0] as s:
                    code = 1
                    if os.fork() == 0:
                        try:
                            _serve_one(s, listener, alive_r, old_handlers)
                        except Exception:
                            sys.excepthook(*sys.exc_info())
                            sys.stderr.flush()
                        finally:
                            os._exit(code)

            except OSError as e:
                if e.errno != errno.ECONNABORTED:
                    raise