Using redis-py to Access an Instance

Access a DCS Redis instance through redis-py on an ECS in the same VPC. For more information on how to use other Redis clients, visit https://redis.io/clients.

The operations described in this section apply only to single-node, master/standby, and Proxy Cluster instances. To connect to a Redis Cluster instance in Python, see https://github.com/Grokzen/redis-py-cluster#usage-example.

Prerequisites

  • The DCS Redis instance you want to access is in the Running state.
  • An ECS has been created to serve as your Redis client. For more information on how to create ECSs, see the Elastic Cloud Server User Guide.
  • If the ECS runs the Linux OS, ensure that the GCC compilation environment has been installed on the ECS.

Procedure

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

    For details, see Viewing Instance Details.

  2. Log in to the ECS.
  3. Install Python and redis-py.

    1. If the system does not provide Python, run the following yum command to install Python:

      yum install python

    2. Run the following command to download and decompress the redis-py package:

      wget https://github.com/andymccurdy/redis-py/archive/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 have been successfully installed if the following command output is displayed:

      Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
      [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import redis
      >>>

  4. Access the DCS Redis instance by using redis-py.

    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:

      Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
      [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import redis
      >>>
    2. Run the following command to access the chosen DCS Redis instance:
      r = redis.StrictRedis(host='192.168.0.171', port=6379, password='******');

      192.168.0.171 is an example IP address/domain name of DCS instance and 6379 is an example port number of DCS instance. For details on how to obtain the IP address/domain name and port, see 1. Change the IP address/domain name and port as required. ****** indicates the password used for login to the chosen DCS Redis instance. This password is defined during DCS Redis instance creation.

      You have successfully accessed the instance if the following command output is displayed:

      Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 
      [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import redis
      >>> r = redis.StrictRedis(host='192.168.0.171', port=6379, password='******');
      >>> 

Video Guide

Watch the following video about accessing a DCS Redis instance.

Accessing a DCS Instance