Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis AUTH command in Python

Tags:

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?

like image 588
Hot.PxL Avatar asked May 10 '15 08:05

Hot.PxL


People also ask

How do I query Redis in Python?

Using Redis-Py package. Create a Python file and add the code shown below to connect to the Redis cluster. Once we have a connection to the server, we can start performing operations. The above example will connect to the database at index 10. The line above will take the first arguments as key and value, respectively.

Does Redis work with Python?

redis-py (which you import as just redis ) is one of many Python clients for Redis, but it has the distinction of being billed as “currently the way to go for Python” by the Redis developers themselves.


2 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.

like image 169
Hot.PxL Avatar answered Sep 18 '22 23:09

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 
like image 37
Punnerud Avatar answered Sep 19 '22 23:09

Punnerud