Redis AUTH command in Python

Question:

I’m using redis-py binding in Python 2 to connect to my Redis server. The server requires a password. I don’t know how to AUTH after making the connection in Python.

The following code does not work:

import redis
r = redis.StrictRedis()
r.auth('pass')

It says:

‘StrictRedis’ object has no attribute ‘auth’

Also,

r = redis.StrictRedis(auth='pass')

does not work either. No such keyword argument.

I’ve used Redis binding in other languages before, and usually the method name coincides with the Redis command. So I would guess r.auth will send AUTH, but unfortunately it does not have this method.

So what is the standard way of AUTH? Also, why call this StrictRedis? What does Strict mean here?

Asked By: Hot.PxL

||

Answers:

Thanks to the hints from the comments. I found the answer from https://redis-py.readthedocs.org/en/latest/.

It says

class redis.StrictRedis(host='localhost', port=6379, db=0, password=None, socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict', unix_socket_path=None)

So AUTH is in fact password passed by keyword argument.

Answered By: Hot.PxL

This worked great for me.

redis_db = redis.StrictRedis(host="localhost", port=6379, db=0, password='yourPassword')

If you have Redis running on a different server, you have to remember to add bind 0.0.0.0 after bind 127.0.0.1 in the config (/etc/redis/redis.conf).
On Ubuntu this should only output one line with 0.0.0.0:

sudo netstat -lnp | grep redis

My result for netstat:

tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      6089/redis-server 0
Answered By: Punnerud

You need to use password instead of AUTH:

Python 2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> r = redis.StrictRedis(host='localhost',port=6379,db=0,password='Prabhat')
>>> print(r)
Redis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
>>>```
Answered By: user3548442

I saw many comments saying Redis does not support username, well it does:

user_connection = redis.Redis(host='localhost', port=6380, username='dvora', password='redis', decode_responses=True)
Answered By: Aymen Alsaadi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.