使用Python客户端(redis-py)连接Redis--华为云DCS for Redis使用经验

使用Python连接Redis,需要先安装Python以及redis-py,以CentOS为例,介绍redis-py的客户端环境搭建。

第0步:准备工作

华为云上购买1台弹性云服务器ECS(我选了CentOS 6.3),一个分布式缓存实例(DCS for Redis),我选了个单机实例。

注意ECS和缓存实例配置相同的VPC和安全组,确保网络互通。

第1步:安装python和redis-py

如果系统没有自带python,可以使用yum方式安装。

yum install python

下载并解压redis-py

wget https://github.com/andymccurdy/redis-py/archive/master.zip;

进入到解压目录后安装redis-py

python setup.py install;

安装后如此验证,不报错说明成功安装redis-py:

[root@ecs-herucentos redis-py-master]# python
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步:连接redis(缓存实例)

命令行模式举例(也可以将命令写入python脚本中再执行):

[root@ecs-herucentos redis-py-master]# python
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='******');
>>> r.set('welcome','Hello, DCS for Redis!');
True
>>> print r.get('welcome');
Hello, DCS for Redis!
>>> exit();
[root@ecs-herucentos redis-py-master]# 

  

猜你喜欢

转载自www.cnblogs.com/husterindg/p/redis-py.html
今日推荐