Redis示例给出HTTP 400:错误的请求错误

时间:2014-05-05 20:19:36

标签: python http caching redis

我正在尝试在我的Cherrypy服务器中缓存MySQL查询。

我在安装pylibmc时无法弄清楚如何解决错误,所以我决定使用Redis-py

我在这里尝试一个非常简单的例子。

import redis
cache = redis.StrictRedis(host='localhost', port=8080, db=0)
       ...
       ...
cache.set('0', '1') # I also tested with other string keys, but failed with same error

并且它抛出了以下错误!

[05/May/2014:13:11:13] HTTP Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/cherrypy/_cprequest.py", line 656, in respond
    response.body = self.handler()
  File "/Library/Python/2.7/site-packages/cherrypy/lib/encoding.py", line 188, in __call__
    self.body = self.oldhandler(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/cherrypy/_cpdispatch.py", line 34, in __call__
    return self.callable(*self.args, **self.kwargs)
  File "server.py", line 92, in submit_data
    cache.set(str(idx), '1')#res)
  File "/Library/Python/2.7/site-packages/redis/client.py", line 897, in set
    return self.execute_command('SET', *pieces)
  File "/Library/Python/2.7/site-packages/redis/client.py", line 461, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "/Library/Python/2.7/site-packages/redis/client.py", line 471, in parse_response
    response = connection.read_response()
  File "/Library/Python/2.7/site-packages/redis/connection.py", line 339, in read_response
    response = self._parser.read_response()
  File "/Library/Python/2.7/site-packages/redis/connection.py", line 118, in read_response
    (str(byte), str(response)))
InvalidResponse: Protocol Error: H, TTP/1.1 400 Bad Request

我无法弄清楚出了什么问题,当我没有使用Redis时,我的网站在8080端口的localhost上运行没有问题。

1 个答案:

答案 0 :(得分:2)

您的网络服务器正在端口8080上运行,而不是您的Redis服务器。您的Redis服务器很可能在端口6379上运行,除非您出于某种原因更改了配置。现在,您正在尝试针对您的网络服务器运行Redis查询,但这不起作用。确保连接到正确的Redis服务器地址和端口,然后重试。