Connection Examples¶
Connecting to a default Redis instance, running locally.¶
[2]:
import redis
connection = redis.Redis()
connection.ping()
[2]:
True
by default Redis return binary responses, to decode them use decode_responses=True¶
[3]:
import redis
decoded_connection = redis.Redis(decode_responses=True)
decoded_connection.ping()
[3]:
True
Connecting to a redis instance, specifying a host and port with credentials.¶
[4]:
import redis
user_connection = redis.Redis(host='localhost', port=6380, username='dvora', password='redis', decode_responses=True)
user_connection.ping()
[4]:
True
Connecting to Redis instances by specifying a URL scheme.¶
Parameters are passed to the following schems, as parameters to the url scheme.
Three URL schemes are supported:
redis://
creates a TCP socket connection. https://www.iana.org/assignments/uri-schemes/prov/redisrediss://
creates a SSL wrapped TCP socket connection. https://www.iana.org/assignments/uri-schemes/prov/redissunix://
: creates a Unix Domain Socket connection.
[7]:
url_connection = redis.from_url("redis://localhost:6379?decode_responses=True&health_check_interval=2")
url_connection.ping()
[7]:
True
Connecting to a Sentinel instance¶
[ ]:
from redis.sentinel import Sentinel
sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1)
sentinel.discover_master("redis-py-test")