Ejemplo n.º 1
0
 def read_from_socket(self, timeout=SENTINEL, raise_on_timeout=True):
     sock = self._sock
     custom_timeout = timeout is not SENTINEL
     try:
         if custom_timeout:
             sock.settimeout(timeout)
         if HIREDIS_USE_BYTE_BUFFER:
             bufflen = recv_into(self._sock, self._buffer)
             if bufflen == 0:
                 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
             self._reader.feed(self._buffer, 0, bufflen)
         else:
             buffer = recv(self._sock, self.socket_read_size)
             # an empty string indicates the server shutdown the socket
             if not isinstance(buffer, bytes) or len(buffer) == 0:
                 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
             self._reader.feed(buffer)
         # data was read from the socket and added to the buffer.
         # return True to indicate that data was read.
         return True
     except blocking_exceptions as ex:
         # if we're in nonblocking mode and the recv raises a
         # blocking error, simply return False indicating that
         # there's no data to be read. otherwise raise the
         # original exception.
         if raise_on_timeout:
             raise
         return False
     except socket.timeout:
         if not raise_on_timeout:
             raise
         return False
     finally:
         if custom_timeout:
             sock.settimeout(self._socket_timeout)
Ejemplo n.º 2
0
    def read_response(self):
        if not self._reader:
            raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)

        # _next_response might be cached from a can_read() call
        if self._next_response is not False:
            response = self._next_response
            self._next_response = False
            return response

        response = self._reader.gets()
        socket_read_size = self.socket_read_size
        while response is False:
            try:
                if HIREDIS_USE_BYTE_BUFFER:
                    bufflen = recv_into(self._sock, self._buffer)
                    if bufflen == 0:
                        raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
                else:
                    buffer = recv(self._sock, socket_read_size)
                    # an empty string indicates the server shutdown the socket
                    if not isinstance(buffer, bytes) or len(buffer) == 0:
                        raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
            except socket.timeout:
                raise TimeoutError("Timeout reading from socket")
            except socket.error:
                e = sys.exc_info()[1]
                raise ConnectionError("Error while reading from socket: %s" %
                                      (e.args,))
            if HIREDIS_USE_BYTE_BUFFER:
                self._reader.feed(self._buffer, 0, bufflen)
            else:
                self._reader.feed(buffer)
            response = self._reader.gets()
        # if an older version of hiredis is installed, we need to attempt
        # to convert ResponseErrors to their appropriate types.
        if not HIREDIS_SUPPORTS_CALLABLE_ERRORS:
            if isinstance(response, ResponseError):
                response = self.parse_error(response.args[0])
            elif isinstance(response, list) and response and \
                    isinstance(response[0], ResponseError):
                response[0] = self.parse_error(response[0].args[0])
        # if the response is a ConnectionError or the response is a list and
        # the first item is a ConnectionError, raise it as something bad
        # happened
        if isinstance(response, ConnectionError):
            raise response
        elif isinstance(response, list) and response and \
                isinstance(response[0], ConnectionError):
            raise response[0]
        return response
Ejemplo n.º 3
0
 def read_from_socket(self, timeout=SENTINEL, raise_on_timeout=True):
     sock = self._sock
     custom_timeout = timeout is not SENTINEL
     try:
         if custom_timeout:
             sock.settimeout(timeout)
         if HIREDIS_USE_BYTE_BUFFER:
             bufflen = recv_into(self._sock, self._buffer)
             if bufflen == 0:
                 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
             self._reader.feed(self._buffer, 0, bufflen)
         else:
             buffer = recv(self._sock, self.socket_read_size)
             # an empty string indicates the server shutdown the socket
             if not isinstance(buffer, bytes) or len(buffer) == 0:
                 raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
             self._reader.feed(buffer)
         # data was read from the socket and added to the buffer.
         # return True to indicate that data was read.
         return True
     except socket.timeout:
         if raise_on_timeout:
             raise TimeoutError("Timeout reading from socket")
         return False
     except NONBLOCKING_EXCEPTIONS as ex:
         # if we're in nonblocking mode and the recv raises a
         # blocking error, simply return False indicating that
         # there's no data to be read. otherwise raise the
         # original exception.
         allowed = NONBLOCKING_EXCEPTION_ERROR_NUMBERS.get(ex.__class__, -1)
         if not raise_on_timeout and ex.errno == allowed:
             return False
         raise ConnectionError("Error while reading from socket: %s" %
                               (ex.args, ))
     finally:
         if custom_timeout:
             sock.settimeout(self._socket_timeout)