python反序列化的坑

82 篇文章 0 订阅

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

 

memcache2 get :1:photo:latest:forward:5855687 cost 2857.6438427
转发 <#59066487>
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。

 


 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值