Access the Redis Instance from a Python client

<p>After you create an ECS instance and a whitelist entry, install a Python Redis client on the ECS instance and access the Redis instance from the Python Redis client.</p> <p><strong><span style="font-size:18px">Prerequisites</span></strong></p> <ol> <li>You have successfully created a Redis instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Redis" target="_blank">Create a Redis Instance</a>. Make sure the instance is in running status.</li> <li>You have successfully created an ECS instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_ECS" target="_blank">Create an ECS Instance</a>. Make sure the instance is in running status.</li> <li>You have successfully created a whitelist entry. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/Getting_Started.Create_Whitelist" target="_blank">Create a Whitelist Entry</a>.</li> </ol> <p><strong><span style="font-size:18px">Usage Guidelines</span></strong></p> <p>This article uses a Linux-CentOS ECS instance as an example.</p> <p><strong><span style="font-size:18px">Procedures</span></strong></p> <p>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Log in to the ECS instance.</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Log in to the <a href="https://pinganyun.com/console/ecs/overview" target="_blank">ECS Console.</a></p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In the left navigation pane, click <strong>Instance</strong> to enter the <strong>Instance Management</strong> page.</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In the <strong>Operations</strong> column for the target instance, click <strong>ECS Console</strong>.</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Enter the username and password.</p> <p>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Configure the editing environment. Execute the following command to install Python:</p> <pre> <code>yum install python</code></pre> <p>3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Install the Python Redis client. For more information, see redis-py-cluster details.</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;a.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Execute the following command to download the Python Redis client:</p> <pre> <code>wget https://github.com/Grokzen/redis-py-cluster/archive/1.3.6.tar.gz</code></pre> <p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507140134-1b6d63009573.png" style="height:20px; margin:0px; width:80px" />: Download redis-py-cluster 1.2.0 or higher.</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Execute the following command to decompress the downloaded Python Redis file:</p> <pre> <code>tar –zxvf 1.3.6.tar.gz</code></pre> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Execute the following command to enter the directory of the decompressed Python Redis file:</p> <pre> <code>cd redis-py-cluster-1.3.6</code></pre> <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;d.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Execute the following command to install the Python Redis client:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p> <pre> <code>python setup.py install</code></pre> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>Result</strong></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Execute the following command to verify that the Python Redis client has been successfully installed and you can enter command-line mode:</p> <pre> <code>python</code></pre> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The system returns the following information, indicating that the installation is successful.</p> <p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507140249-1a85ca6f94c0.png" style="height:97px; width:830px" /></p> <p>4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Connect to the Redis instance.</p> <p>Execute the following commands to connect to the Redis instance:</p> <pre> <code>from rediscluster import StrictRedisCluster startup_nodes = [{"host": "Domain-Name", "port": "Port"}] rc = StrictRedisCluster(startup_nodes=startup_nodes,password='Password',max_connections=32, max_connections_per_node=True,socket_timeout=5000,socket_connect_timeout=3000,retry_on_timeout=True ) rc.set("foo", "pingan yun")</code></pre> <p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507140134-1b6d63009573.png" style="height:20px; margin:0px; width:80px" />:</p> <ul> <li>Domain-Name: Access domain name of the Redis instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">View Basic Information about an Instance</a>.</li> <li>Port: Port number of the Redis instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">View Basic Information about an Instance</a>.</li> <li>Password: Password of the Redis instance that has been set when you create the Redis instance. For more information, see <a href="http://pinganyun.com/ssr/help/database/Redis/User_Guide.Instance_Management.View_Instance" target="_blank">View Basic Information about an Instance</a>.</li> </ul> <p><strong>Result</strong></p> <p>Execute the following command to view the newly written data in the Redis instance:</p> <pre> <code>print(rc.get("foo"))</code></pre> <p>The system returns pingan yun, indicating that the connection succeeds.</p> <p><img src="https://obs-cn-shanghai.yun.pingan.com/pcp-portal/20201507140422-197ec5e49751.png" style="height:71px; width:306px" />&nbsp;</p>
Did the above content solve your problem? Yes No
Please complete information!

Call us

400-151-8800

Email us

cloud@pingan.com

Online customer service

Instant reply

Technical Support

cloud products