Python'获取'上的未知标志查询memcached时出错

时间:2017-11-01 15:41:03

标签: python memcached

我有非常直接的Python代码,它从memcached查询数据:

import memcache 
client = memcache.Client([('127.0.0.1', 11211)])
res = client.get("data_e")

data_e 键存在,不为空,我可以通过telnet连接到memcached获得结果。 但是,当执行Python脚本时,我一直收到以下错误:

Traceback (most recent call last):
  File "./position_expiration.py", line 5, in <module>
    res = client.get("data_e")
  File "/usr/lib/python2.7/site-packages/memcache.py", line 1129, in get
    return self._get('get', key)
  File "/usr/lib/python2.7/site-packages/memcache.py", line 1113, in _get
    return _unsafe_get()
  File "/usr/lib/python2.7/site-packages/memcache.py", line 1101, in _unsafe_get
    value = self._recv_value(server, flags, rlen)
  File "/usr/lib/python2.7/site-packages/memcache.py", line 1294, in _recv_value
    raise ValueError('Unknown flags on get: %x' % flags)
ValueError: Unknown flags on get: 20

memcached中的数据由第三方服务存储,因此我无法改变在那里写入的方式。我可以从客户端使用Python做什么来阅读它?

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

这是中文的解决方案:https://www.cnblogs.com/LanTianYou/p/9204431.html

解决方案是在python文件“ memcache.py”中的方法“ _recv_value”中修改最终的else块内容。

_recv_value方法应如下所示:

def _recv_value(self, server, flags, rlen):
    rlen += 2  # include \r\n
    buf = server.recv(rlen)
    if len(buf) != rlen:
        raise _Error("received %d bytes when expecting %d"
                     % (len(buf), rlen))

    if len(buf) == rlen:
        buf = buf[:-2]  # strip \r\n

    if flags & Client._FLAG_COMPRESSED:
        buf = self.decompressor(buf)
        flags &= ~Client._FLAG_COMPRESSED
    if flags == 0:
        # Bare bytes
        val = buf
    elif flags & Client._FLAG_TEXT:
        val = buf.decode('utf-8')
    elif flags & Client._FLAG_INTEGER:
        val = int(buf)
    elif flags & Client._FLAG_LONG:
        if six.PY3:
            val = int(buf)
        else:
            val = long(buf)  # noqa: F821
    elif flags & Client._FLAG_PICKLE:
        try:
            file = BytesIO(buf)
            unpickler = self.unpickler(file)
            if self.persistent_load:
                unpickler.persistent_load = self.persistent_load
            val = unpickler.load()
        except Exception as e:
            self.debuglog('Pickle error: %s\n' % e)
            return None
    else:
        self.debuglog("unknown flags on get: %x\n" % flags)
        # 注释掉这行
        # raise ValueError('Unknown flags on get: %x' % flags)
        # 设定返回值
        val = buf

    return val