zoukankan      html  css  js  c++  java
  • Python客户端(redis-py)连接Redis

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

    以下内容的验证环境为华为云的分布式缓存(DCS for Redis)以及弹性云服务器ECS。

    第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

    unzip redis-py-master.zip
    cd redis-py-master
    python3 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(缓存实例)

    命令行模式使用redis-py举例(也可以将命令写入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]#

     

  • 相关阅读:
    TDD
    算法与数据结构 文档 1 洋洋洋传
    编程的专精度
    python小课
    有时心情舒畅时打个代码心里都是默默地同步输出...
    同时可以运行在JVM上的Kotlin~枚举和判定以及数据对象的写法总结
    map
    multiset
    set
    priority_queue
  • 原文地址:https://www.cnblogs.com/longfeiwang/p/11072994.html
Copyright © 2011-2022 走看看