python 反序列化_python反序列化的坑

今天遇到python一个坑,调用memcached的get_man报错:

memcache2 get :1:photo:latest:forward:5855687 cost 2857.6438427

转发

unpickle :1:photo:latest:forward:5855687 FAIL __import__() argument 1 must be string without null bytes, not str

同样一个key,一个是通过get,一个是通过get_many,结果报错。code如下:

# -*- coding: utf-8 -*-

from django.core.management import setup_environ

import settings

setup_environ(settings)

key = 'photo:latest:forward:5855687'

from django.core.cache import cache

print cache.get(key)

print cache.get_many([key])

跟进代码发现问题出在get_man的反序列化上:

def get_multi(self,key):

result = cacheService.getMulti(key)

if result is None:

return []

else:

result_dict = {}

for k,val in result.items():

try:

file = StringIO(val)

_up = unpickler(file)

val = _up.load()

result_dict[k] = val

except Exception,e:

print 'unpickle %s FAIL %s'%(k,e)

pass

return result_dict

尝试把序列化改成:

result_dict[k] = pickle.loads(val)

报错信息变了:

unpickle :1:photo:latest:forward:5855687 FAIL  must be string, not unicode

难道是val的type不是str,是unicode? 通过print type(val) 打印出来,果然是unicode。

23eb84a2cae4ec2a0fad889bd806da3f.png

f9b01cc50c3df1570d22fc714710a0dc.png

大小: 43.5 KB

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2013-01-06 18:06

浏览 889

分类:非技术

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值