1 安装redis的三种方式
(1) pip install redis
(2) easy_install redis
(3) 下载源码包
获取安装包:
wget –no-check-certificate https://pypi.python.org/packages/source/r/redis/redis-2.8.0.tar.gz
解压包:
tar -zvxf redis-2.8.0.tar.gz
建立软链:
ln -s redis-2.8.0 python-redis
安装:
cd python-redis && python setup.py install
2 调用模块
通过init创建对象,指定参数host,port与指定服务器和端口连接,host默认为localhost,port默认为6379,db默认为0
举例:字符串串的增删改查
# coding=utf-8
from redis import StrictRedis
if __name__ == '__main__':
# 创建一个strictRedis对象,链接redis数据库
try:
sr = StrictRedis()
# add key name zhangsan
res = sr.set('name','zhangsan')
print(res)
res = sr.get('name')
print(res)
res = sr.set('name','wanger')
res = sr.get('name')
print(res)
res = sr.set('age','18')
res = sr.get('age')
print(res)
res = sr.delete('age')
print(res)
res = sr.keys()
print(res)
except Exception as e:
print(e)