Updated on 2026-04-10 GMT+08:00

Connecting to Redis on redis-py (Python)

This section describes how to access a Redis instance on redis-py. For more information about how to use other Redis clients, visit the Redis official website.

The following operations are based on an example of accessing a Redis instance on a client on an Elastic Cloud Server (ECS).

Notes and Constraints

For DCS Redis 7.0 instances, 5.0.0 and later are recommended.

Prerequisites

Procedure

  1. View the IP address/domain name and port of the DCS Redis instance to be accessed.

    For details, see Viewing and Modifying Basic Settings of a DCS Instance.

  2. Log in to the ECS.

    The following uses CentOS as an example to describe how to access an instance using a Python client.

  3. Access the DCS Redis instance.

    1. If the ECS OS does not provide Python, run the following yum command to install it:
      yum install python

      The Python version must be 3.6 or later. If the default Python version is earlier than 3.6, perform the following operations to change it:

      1. Run the rm -rf python command to delete the Python symbolic link.
      2. Run the ln -s pythonX.X.X python command to create another Python link. In the command, X.X.X indicates the Python version number.
    2. Install Python and redis-py.
      1. If the system does not provide Python, run the yum command to install it.
      2. Run the following command to download and decompress the redis-py package:
        wget https://github.com/andymccurdy/redis-py/archive/master.zip
        unzip master.zip
      3. Go to the directory where the decompressed redis-py package is saved, and install redis-py.
        python setup.py install

        After the installation, run the python command. redis-py has been successfully installed if the following command output is displayed:

        Figure 1 Running the python command
    3. Use the redis-py client to connect to the instance. In the following steps, commands are executed in CLI mode. (Alternatively, write the commands into a Python script and then execute the script.)
      1. Run the python command to enter the CLI mode. You have entered CLI mode if the following command output is displayed:
        Figure 2 Entering the CLI mode
      2. Run the following command to access the chosen DCS Redis instance:

        Single-node, master/standby, read/write splitting, or Proxy Cluster:

        >>> import redis
        >>> r = redis.StrictRedis(host='XXX.XXX.XXX.XXX', port=6379, password='******', socket_timeout=5, socket_connect_timeout=5)
        >>> rc.set("foo", "bar")
        True
        >>> print(rc.get("foo"))
        'bar'
        >>>

        XXX.XXX.XXX.XXX indicates the IP address/domain name of the DCS instance and 6379 is an example port number of the instance. For details about how to obtain the IP address/domain name and port, see 1. Change them as required. ****** indicates the password used for logging in to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation. To connect to an instance using an ACL user, set the instance password to username:password. For details about how to create or view an ACL user, see Configuring DCS Redis ACL Users. Omit the , password='******' part in the command for a password-free instance.

        socket_timeout indicates the response-waiting timeout interval (in seconds). socket_connect_timeout indicates the connection timeout interval (in seconds). The default is None. The recommended configuration is 5s.

        You have successfully accessed the instance if the following command output is displayed. Enter commands to perform read and write operations on the database.

        Cluster:

        >>> import redis
        >>> startup_nodes = [redis.cluster.ClusterNode("192.168.0.143", "6379"),redis.cluster.ClusterNode("192.168.0.144", "6379"),redis.cluster.ClusterNode("192.168.0.145", "6379")]
        >>> rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True, password='******', socket_timeout=5, socket_connect_timeout=5)
        >>> rc.set("foo", "bar")
        True
        >>> print(rc.get("foo"))
        'bar'
        >>>

Related Document

When accessing Redis fails, see Troubleshooting Redis Connection Failures.