今天发现,使用redis-py从redis中获取的数据竟然是加密的。

conn = redis.Redis(host='redis_serverip', port=6379, password='redis_password', db=0)

conn.set('string-key1', 'value1')

result = conn.get('string-key1')
print('result:', result)
print('result type:', type(result))
print() result_decoded = result.decode('utf-8')
print('result_decoded:', result_decoded)
print('result_decoded type:', type(result_decoded))
print() result_str = str(result)
print('result_str:', result_str)
print('result_str type:', type(result_str))
print() print('test int in redis...\n') conn.set('string-key2', 100)
result = conn.get('string-key2')
print('result:', result)
print('result type:', type(result))
print() result_decoded = int(result)
print('result_decoded:', result_decoded)
print('result_decoded type:', type(result_decoded))

输出:

result: b'value1'
result type: <class 'bytes'>

result_decoded: value1
result_decoded type: <class 'str'>

result_str: b'value1'
result_str type: <class 'str'>

test int in redis...

result: b'100'
result type: <class 'bytes'>

result_decoded: 100
result_decoded type: <class 'int'>

可以看出:

如果传入str,传出的是bytes,需要通过decode('utf-8')进行恢复。

如果传入int,传出的是bytes,需要通过int()进行恢复。

另一种解决办法是在创建redis client时,进行decode设置。

conn = redis.Redis(host='redis_serverip', port=6379, password='redis_password', db=0, decode_responses=True)

conn.set('string-key3', 'value3')

result = conn.get('string-key3')
print('result:', result)
print('result type:', type(result))
print() print('test int in redis...\n') conn.set('string-key4', 100) result = conn.get('string-key4')
print('result:', result)
print('result type:', type(result))
print() result_decoded = int(result)
print('result_decoded:', result_decoded)
print('result_decoded type:', type(result_decoded))

输出:

result: value3
result type: <class 'str'>

test int in redis...

result: 100
result type: <class 'str'>

result_decoded: 100
result_decoded type: <class 'int'>

这种情况下,str是正常的,int需要注意一下。

参考资料:

Problem with hash get/set

redis-py中的坑的更多相关文章

  1. 分布式爬虫scrapy-redis中settings.py中的配置信息

    SCHEDULER = "scrapy_redis.scheduler.Scheduler" # 使用scrapy-redis的调度器 ITEM_PIPELINES = { 'sc ...

  2. php安装扩展redis淌过的坑

    php安装扩展redis淌过的坑 php扩展 redis cd /usr/local/src/ wget https://github.com/nicolasff/phpredis/archive/2 ...

  3. Torch-RNN运行过程中的坑 [2](Lua的string sub函数,读取中文失败,乱码?)

    0.踩坑背景 仍然是torch-rnn/LanguageModel.lua文件中的一些问题,仍然是这个狗血的LM:encode_string函数: function LM:encode_string( ...

  4. linux上部署redis实现与Python上的redis交互(有坑)

    1.概念 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件 2.linux安装redis 2.1yum源安装redis(不推荐) #前提得配置好阿里 ...

  5. 饿了么这样跳过Redis Cluster遇到的“坑”

    内容来源:2017 年 8 月 12 日,饿了么高级Python工程师黄光星在“CRUG 2017北京活动”进行<Redis Cluster运维方案>演讲分享.IT 大咖说(微信id:it ...

  6. 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:

    决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...

  7. python调用py中rar的路径问题。

    1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...

  8. Django 中 如何使用 settings.py 中的常量

    在用django 框架开发 python web 程序的时候 , 在模板页面经常会用到 settings.py 中设置的常量,比如MEDIA_URL, 我尝试过在模板页面用类似如下的方式 程序代码 { ...

  9. PHP redis Api 中文文档

    phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...

  10. django访问sqlserver中的坑

    首先不用说先安装django-sqlserver    pip install django-sqlserver 然后在settings.py中修改'ENGINE': 'sqlserver_ado', ...

随机推荐

  1. springboot https证书配置

    如果公司有提供证书如: 拿到证书秘钥可直接在springboot 的配置文件中配置: server.ssl.key-store=classpath:cert.pfx server.ssl.key-st ...

  2. https的秘钥公钥以及之间的会话流程

      一 共享秘钥 1.1 概念 共享秘钥和我们生活中同一把锁的钥匙概念类似,对同一把锁来说,加锁时使用什么钥匙,解锁也必须使用同样的钥匙. 1.2 共享秘钥在HTTP传输中的缺点 以共享密钥方式加密时 ...

  3. 用Jenkins集成ios项目设置多scheme,同一代码自动输出多个环境包 实现便捷切换API环境

    Jenkins 安装使用参考我的博客http://www.cnblogs.com/zhujin/p/9064820.html Xcode 配置:说明 一个schema 对应一套环境(如生产,测试),一 ...

  4. 【three.js 第一课】创建场景,显示几何体

    <!DOCTYPE html> <html> <head> <title>demo1</title> </head> <s ...

  5. Treasure Island DFS +存图

    All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...

  6. cucumber学习索引

    Cucumber(1) —— 环境配置 Cucumber(2)——目录结构以及基本语法 Cucumber(3)——命令以及日志 Cucumber(4)——jenkins的集成

  7. vue2.x学习笔记(九)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12577948.html. 数组的更新检测 数组在javascript是一种特殊的对象,不是像普通的对象那样通过Ob ...

  8. MergeSort归并排序和利用归并排序计算出数组中的逆序对

    首先先上LeetCode今天的每日一题(面试题51. 数组中的逆序对): 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. ...

  9. 基于MySQL Binlog的Elasticsearch数据同步实践

    一.为什么要做 随着马蜂窝的逐渐发展,我们的业务数据越来越多,单纯使用 MySQL 已经不能满足我们的数据查询需求,例如对于商品.订单等数据的多维度检索. 使用 Elasticsearch 存储业务数 ...

  10. tensorflow1.0 构建神经网络做非线性归回

    """ Please note, this code is only for python 3+. If you are using python 2+, please ...