Python交互

  • 安装包如下
pip install redis-py-cluster
#coding=utf-8
from rediscluster import StrictRedisCluster

if __name__=="__main__":
    try:
        #构建所有的节点,Redis会使用CRC16算法,将键和值写到某个节点上
        startup_nodes=[
            {'host': '172.16.0.136', 'port': '7000'},
            {'host': '172.16.0.135', 'port': '7003'},
            {'host': '172.16.0.136', 'port': '7001'},
        ]
        #构建StrictRedisCluster对象
        rc=StrictRedisCluster(startup_nodes=startup_nodes,decode_responses=True)
        #设置键为py2、值为hr的数据
        rc.set('py2','hr')
        #获取键为py2的数据并输出
        print rc.get('py2')
    except Exception as e:
        print e