示例#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)
示例#2
0
    def _read_from_socket(self, length=None):
        socket_read_size = self.socket_read_size
        buf = self._buffer
        buf.seek(self.bytes_written)
        marker = 0

        try:
            while True:
                data = recv(self._sock, socket_read_size)
                # an empty string indicates the server shutdown the socket
                if isinstance(data, bytes) and len(data) == 0:
                    raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
                buf.write(data)
                data_length = len(data)
                self.bytes_written += data_length
                marker += data_length

                if length is not None and length > marker:
                    continue
                break
        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,))
示例#3
0
    def _read_from_socket(self, length=None):
        socket_read_size = self.socket_read_size
        buf = self._buffer
        buf.seek(self.bytes_written)  # TODO 指针跳到buffer中已写bytes的末尾
        marker = 0  # TODO 用来记录已经从socket中接收到的报文的总长度

        try:
            # TODO 如果length为None,则while循环仅仅只执行了1次
            while True:
                data = recv(self._sock,
                            socket_read_size)  # TODO 从socket中读取指定长度的数据报文
                # an empty string indicates the server shutdown the socket
                if isinstance(data, bytes) and len(data) == 0:
                    raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
                buf.write(data)  # TODO 将读取到的response数据写到self._buffer
                data_length = len(data)
                self.bytes_written += data_length
                marker += data_length

                if length is not None and length > marker:  # TODO 当接收的报文长度小于length时,才继续接收
                    continue
                break
        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, ))
示例#4
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
示例#5
0
    def _read_from_socket(self,
                          length=None,
                          timeout=SENTINEL,
                          raise_on_timeout=True):
        sock = self._sock
        socket_read_size = self.socket_read_size
        buf = self._buffer
        buf.seek(self.bytes_written)
        marker = 0
        custom_timeout = timeout is not SENTINEL

        try:
            if custom_timeout:
                sock.settimeout(timeout)
            while True:
                data = recv(self._sock, socket_read_size)
                # an empty string indicates the server shutdown the socket
                if isinstance(data, bytes) and len(data) == 0:
                    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
                buf.write(data)
                data_length = len(data)
                self.bytes_written += data_length
                marker += data_length

                if length is not None and length > marker:
                    continue
                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)
示例#6
0
    def _read_from_socket(self, length=None):
        socket_read_size = self.socket_read_size
        buf = self._buffer
        buf.seek(self.bytes_written)
        marker = 0

        while True:
            data = recv(self._sock, socket_read_size)
            # an empty string indicates the server shutdown the socket
            if isinstance(data, bytes) and len(data) == 0:
                raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
            buf.write(data)
            data_length = len(data)
            self.bytes_written += data_length
            marker += data_length

            if length is not None and length > marker:
                continue
            break
示例#7
0
    def _read_from_socket(self, length=None):
        socket_read_size = self.socket_read_size
        buf = self._buffer
        buf.seek(self.bytes_written)
        marker = 0

        while True:
            data = recv(self._sock, socket_read_size)
            # an empty string indicates the server shutdown the socket
            if isinstance(data, bytes) and len(data) == 0:
                raise socket.error(SERVER_CLOSED_CONNECTION_ERROR)
            buf.write(data)
            data_length = len(data)
            self.bytes_written += data_length
            marker += data_length

            if length is not None and length > marker:
                continue
            break
    def _read_from_socket(self,
                          length=None,
                          timeout=SENTINEL,
                          raise_on_timeout=True):
        sock = self._sock
        socket_read_size = self.socket_read_size
        buf = self._buffer
        buf.seek(self.bytes_written)
        marker = 0
        custom_timeout = timeout is not SENTINEL

        try:
            if custom_timeout:
                sock.settimeout(timeout)
            while True:
                data = recv(self._sock, socket_read_size)
                # an empty string indicates the server shutdown the socket
                if isinstance(data, bytes) and len(data) == 0:
                    raise ConnectionError(SERVER_CLOSED_CONNECTION_ERROR)
                buf.write(data)
                data_length = len(data)
                self.bytes_written += data_length
                marker += data_length

                if length is not None and length > marker:
                    continue
                return True
        except BlockingIOError 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 or ex.errno != EWOULDBLOCK:
                raise
            return False
        except socket.timeout:
            if raise_on_timeout:
                raise
            return False
        finally:
            if custom_timeout:
                sock.settimeout(self.socket_timeout)
示例#9
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)