python - Redis 示例给出 HTTP 400 : Bad request error

标签 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 端口的本地主机上运行没有问题。

最佳答案

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

关于python - Redis 示例给出 HTTP 400 : Bad request error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23481089/

相关文章:

java - 当缩放级别为 100% 时,如何单击 UI 上不可见的元素?

python - 如何使用 python OpenCV 在匹配特定值的单 channel 图像中查找最大连通分量?

node.js - 两个http请求能走到一起吗?如果可以,nodeJS 服务器如何处理它?

c# - 企业库异常: Activation error occured while trying to get instance of type ICacheManager, key ?

python - Anjuta IDE - 简单的 Python 问题

java - 如何在解析为 dto 之前获取 http Jersey 响应状态?

http - 使用 Pentaho Kettle,我如何自动重试由于连接中断而失败的休息请求?

.net - 如何通过 Powershell 在 App Fabric 上创建区域

javascript - 防止使用 javascript 缓存图像

python - 退出失败的脚本运行(python)