L-Jason

do sth you need, then do sth you want 。。。

Python 连接redis密码中特殊字符问题

连接方法:

self.pool = redis.ConnectionPool.from_url(self.redis_url)
opredis = redis.Redis(connection_pool=self.pool)
redis_url = 'redis://:cot$#D4^&1234@172.31.26.174:6379/0'

直接连redis会报错,报错主要内容:

ValueError: invalid literal for int() with base 10

问题:redis密码中不允许有特殊字符 ? #,具体请查看ConnectionPool.from_url方法

解决方法:

配置密码是对密码encode,连接时对密码decode

以python3为例:

from urllib import parse

redis_url = 'redis://:{}@172.31.26.174:6379/0'.format(parse.quote('cot$#D4^&1234'))

self.pool = redis.ConnectionPool.from_url(self.redis_url, , decode_components=True) 

opredis = redis.Redis(connection_pool=self.pool)

posted @ 2019-09-16 17:52  L-Jason  阅读(4168)  评论(0编辑  收藏  举报