温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python2.7连接操作redis的几种方法

发布时间:2020-08-10 11:59:17 来源:ITPUB博客 阅读:280 作者:chenfeng 栏目:编程语言
python版本:2.7
首先需要先安装redis模块:
wget https://pypi.python.org/packages/source/r/redis/redis-2.10.3.tar.gz
tar zxvf redis-2.10.3.tar.gz
cd redis-2.10.3
python setup.py install 
备注:安装方法可以参考INSTALL文件内容
#cat INSTALL
Please use
 python setup.py install
and report errors to Andy McCurdy (sedrik@gmail.com)


然后开始导入redis模块
[root@test ~/python]#python
Python 2.7.12 (default, Jun  1 2018, 14:00:01) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import redis
>>> 

以下是操作redis的四种方法:
#cat 1.py
#encoding=utf-8   
import redis
r =  redis.StrictRedis(host='localhost', port=6379)
print r.ping()


#cat 2.py
#encoding=utf-8
import redis
r = redis.StrictRedis(host='127.0.0.1', port=6379, db=0)
print r.ping()


#cat 3.py
#encoding=utf-8
import redis
pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)
r = redis.Redis(connection_pool=pool)
print r.ping()


#cat 4.py
# -*- encoding: UTF-8 -*-  
import redis 
r = redis.Redis(host='localhost', port=6379, decode_responses=True)  
print r.ping()
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI