Redis AUTH command in Python
Asked Answered
P

4

34

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?

Pallaton answered 10/5, 2015 at 8:51 Comment(3)
Rather poor effort on documenting the project, it seems. On the GH page and on pypi there's no explicit link to the official documentation. I had to Google it to find redis-py.readthedocs.org/en/latest and kushal.fedorapeople.org/redis-py/html/index.html. Both are automatically generated from the code.Deenadeenya
the only place where they mention authentication is: on_connect() Initialize the connection, authenticate and select a database. Ask andymccurdy, Redis says redis-py is mature and supported, the way to go.Deenadeenya
@Deenadeenya Thanks! The doc solved it. Please find below.Pallaton
P
54

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.

Pallaton answered 10/5, 2015 at 15:47 Comment(6)
good, well-done! I'm glad I pointed you to the right direction.Deenadeenya
what about username? I can't find username anywhere.Falkner
@JustinThomas What username? Does redis have a username? I haven't used it ever since.Pallaton
@JustinThomas there is no username for redisAberdeen
Thanks for posting this, helped me too!Determination
To clear things up for newcommers: If you do not provide a username, it will try to authenticate the default user. There can however be multiple users.Interpretation
A
8

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
Amylaceous answered 22/4, 2019 at 12:13 Comment(0)
L
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>>>
>>>```
Leyba answered 24/6, 2020 at 20:29 Comment(0)
R
3

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)
Razorbill answered 24/3, 2022 at 15:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.