Redis示例返回HTTP 400:错误请求

1 投票
1 回答
4679 浏览
提问于 2025-04-18 05:28

我正在尝试在我的 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 端口上运行得很好。

1 个回答

2

你的网页服务器是在8080端口上运行的,而不是Redis服务器。通常情况下,Redis服务器是在6379端口上运行的,除非你出于某种原因修改了配置。现在你正在尝试对网页服务器发送Redis的查询,这样是行不通的。请确保你连接的是正确的Redis服务器地址和端口,然后再试一次。

撰写回答