'Redis AUTH command in Python

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?



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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>>>
>>>```

Solution 4:[4]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hot.PxL
Solution 2 Punnerud
Solution 3 user3548442
Solution 4 Aymen Alsaadi