Raman
Raman

Reputation: 717

Cannot create redis client (authentication against Cluster)

Unable to connect to redis cluster using username/password.

from redis.cluster import RedisCluster, ClusterNode

def get_redis_connection():
  url = f"rediss://{uname}:{passwd}@{host}:{port}/0"
  connection = RedisCluster.from_url(url)
  if connection.ping():
     print("Success")
  else:
     print("Failed")

Error occurred in get_redis_connection: invalid literal for int() with base 10:

Note: Password contains alphanumeric and special characters, including quotes. Setup: pip install redis

Assume, user name and password is like below, where username is "travel-tire":

rediss://travel-tire:v'Y:[4o2n:3*s@hostname:port

Upvotes: 1

Views: 559

Answers (1)

Zac Anger
Zac Anger

Reputation: 7787

You have characters in your password that aren't valid URI components.

from redis.cluster import RedisCluster, ClusterNode
# add this import
from urllib.parse import quote


def get_redis_connection():
    # encode your password to make it valid for a uri
    password = quote(passwd, safe="")
    url = f"rediss://{uname}:{passwd}@{password}:{port}/0"
    connection = RedisCluster.from_url(url)
    if connection.ping():
        print("Success")
    else:
        print("Failed")

Depending on your version of the Redis client (if you're on a version older than 4.0.0) you'll also need to change the from_url call:

    connection = RedisCluster.from_url(url, decode_components=True)

Upvotes: 2

Related Questions