Example #1
0
    def __call__(self, timeout=None):
        """
        Accept a new connection

        :Parameters:
         - `timeout`: Timeout in seconds

        :Types:
         - `timeout`: ``float``

        :return: New socket and the peername
        :rtype: ``tuple``

        :Exceptions:
         - `SocketTimeout`: accept call timed out
         - `SocketError`: An error occured while accepting the socket
        """
        while True:
            try:
                sock, peer = self._accept(timeout)
            except _socket.error, e:
                if e[0] in self._IGNOREFAIL:
                    continue
                e = _sys.exc_info()
                try:
                    raise SocketError, e[1], e[2]
                finally:
                    del e
            _osutil.close_on_exec(sock.fileno())
            return sock, peer
Example #2
0
    def _open(self):
        """
        Open the pid file

        :return: The file object
        :rtype: ``file``

        :Exceptions:
         - `PidfileError`: The file could not be opened
        """
        fp = self._fp
        if fp is not None:
            return fp
        try:
            fd = _osutil.safe_fd(_os.open(self.name, self._O_FLAGS, 0666))
            try:
                _osutil.close_on_exec(fd)
                return _os.fdopen(fd, 'w+')
            except: # pylint: disable = W0702
                e = _sys.exc_info()
                try:
                    _os.close(fd)
                finally:
                    try:
                        raise e[0], e[1], e[2]
                    finally:
                        del e
        except (OSError, IOError), e:
            raise PidfileError(str(e))
Example #3
0
    def _finalize_listeners(self, msg):
        """
        Finalize the listening sockets

        This method actually sets the sockets to the LISTEN state.

        :Parameters:
         - `msg`: Configuration error message template

        :Types:
         - `msg`: ``str``

        :return: Socket acceptor
        :rtype: ``callable``

        :Exceptions:
         - `ConfigurationError`: No listeners available
        """
        if not self._sockets:
            raise ConfigurationError("No listener sockets")

        memory, toremove = {}, []
        for socket in sorted(self._sockets):
            if socket.key() in memory or socket.anykey() in memory:
                # Do not issue the warning on any-ipv4/ipv6 inclusion
                if socket.key() != socket.anykey() or \
                        memory[socket.key()] == socket.family():
                    _warnings.warn("Duplicate listen: %s" % (socket.bindspec),
                        category=ListenerWarning)
                toremove.append(socket)
                continue
            _osutil.close_on_exec(socket)
            socket.setblocking(False)
            try:
                socket.bind()
                socket.listen(_socket.SOMAXCONN)
            except _socket.error, e:
                stre = str(e)
                e = _sys.exc_info()
                try:
                    raise ConfigurationError, \
                        (msg % socket.bindspec) + ": " + stre, e[2]
                finally:
                    del e
            else:
                memory[socket.key()] = socket.family()