def read_response(self, command_name, catch_errors): response = self.read().strip() # strip last two characters (\r\n) if not response: self.disconnect() raise ConnectionError("Socket closed on remote end") # server returned a null value if response in ('$-1', '*-1'): return None byte, response = response[0], response[1:] # server returned an error if byte == '-': if response.startswith('ERR '): response = response[4:] raise ResponseError(response) # single value elif byte == '+': return response # int value elif byte == ':': return long(response) # bulk response elif byte == '$': length = int(response) if length == -1: return None response = length and self.read(length) or '' self.read(2) # read the \r\n delimiter return response # multi-bulk response elif byte == '*': length = int(response) if length == -1: return None if not catch_errors: return [ self.read_response(command_name, catch_errors) for i in range(length) ] else: # for pipelines, we need to read everything, # including response errors. otherwise we'd # completely mess up the receive buffer data = [] for i in range(length): try: data.append( self.read_response(command_name, catch_errors)) except Exception: e = sys.exc_info()[1] data.append(e) return data raise InvalidResponse("Unknown response type for: %s" % command_name)
def read_response(self, command_name, catch_errors): response = self.read().strip() # strip last two characters (\r\n) if not response: self.disconnect() raise ConnectionError("Socket closed on remote end") # server returned a null value if response in ('$-1', '*-1'): return None byte, response = response[0], response[1:] # server returned an error if byte == '-': if response.startswith('ERR '): response = response[4:] raise ResponseError(response) # single value elif byte == '+': return response # int value elif byte == ':': return long(response) # bulk response elif byte == '$': length = int(response) if length == -1: return None response = length and self.read(length) or '' self.read(2) # read the \r\n delimiter return response # multi-bulk response elif byte == '*': length = int(response) if length == -1: return None if not catch_errors: return [self.read_response(command_name, catch_errors) for i in range(length)] else: # for pipelines, we need to read everything, # including response errors. otherwise we'd # completely mess up the receive buffer data = [] for i in range(length): try: data.append( self.read_response(command_name, catch_errors) ) except Exception: e = sys.exc_info()[1] data.append(e) return data raise InvalidResponse("Unknown response type for: %s" % command_name)
def read_response(self): response = self.read() if not response: raise ConnectionError("Socket closed on remote end") byte, response = response[0], response[1:] if MAJOR_VERSION >= 3: byte = unichr(byte) # server returned an error if byte == '-': if response.startswith('ERR '.encode()): response = response[4:] return ResponseError(response) if response.startswith('LOADING '.encode()): # If we're loading the dataset into memory, kill the socket # so we re-initialize (and re-SELECT) next time. raise ConnectionError("Redis is loading data into memory") # single value elif byte == '+': return response # int value elif byte == ':': return long(response) # bulk response elif byte == '$': length = int(response) if length == -1: return None response = self.read(length) #Start Modify By Sky response = str(response, encoding = "utf-8") #End Modify By Sky return response # multi-bulk response elif byte == '*': length = int(response) if length == -1: return None return [self.read_response() for i in xrange(length)] raise InvalidResponse("Protocol Error")