Python连接Redis的基本配置方法


Posted in Python onSeptember 13, 2018

在Linux系统下Python连接Redis的基本配置方法具体操作步骤

系统环境:

OS:Oracle Linux Enterprise 5.6

Redis:redis-2.6.8

Python:Python-2.7.3

redis的python包版本:redis-2.7.2.tar

前提条件:

1.确保Redis已成功安装并且正确配置,参考文档

主从配置文档:

2.确保Python环境已成功配置,参考文档

配置python连接redis:

1.安装Redis的Python包:

使用easy-install安装,关于easy-install的配置,参考以上Python环境的搭建。

[root@njdyw bin]# easy_install2.7.3 redis
Searching for redis
Reading http://pypi.python.org/simple/redis/
Reading http://github.com/andymccurdy/redis-py
Best match: redis 2.7.2
Downloading http://pypi.python.org/packages/source/r/redis/redis-2.7.2.tar.gz#md5=17ac60dcf13eb33f82cc25974ab17157
Processing redis-2.7.2.tar.gz
Running redis-2.7.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-8FAlft/redis-2.7.2/egg-dist-tmp-JzQViJ
zip_safe flag not set; analyzing archive contents...
Adding redis 2.7.2 to easy-install.pth file
 
Installed /usr/local/python2.7.3/lib/python2.7/site-packages/redis-2.7.2-py2.7.egg
Processing dependencies for redis
Finished processing dependencies for redis

安装Parser包(可选)

说明:Parser可以控制如何解析redis响应的内容。redis-py包含两个Parser类,PythonParser和HiredisParser。默认,如果已经安装了hiredis模块,redis-py会使用HiredisParser,否则会使用PythonParser。

HiredisParser是C编写的,由redis核心团队维护,性能要比PythonParser提高10倍以上,所以推荐使用。安装方法,使用easy_install:

[root@njdyw ~]# easy_install2.7.3 hiredis
Searching for hiredis
Reading http://pypi.python.org/simple/hiredis/
Reading https://github.com/pietern/hiredis-py
Best match: hiredis 0.1.1
Downloading http://pypi.python.org/packages/source/h/hiredis/hiredis-0.1.1.tar.gz#md5=92128474f6fb027cfb8587fce724ea8e
Processing hiredis-0.1.1.tar.gz
Running hiredis-0.1.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ZanSCB/hiredis-0.1.1/egg-dist-tmp-XCZBQ0
zip_safe flag not set; analyzing archive contents...
Adding hiredis 0.1.1 to easy-install.pth file
 
Installed /usr/local/python2.7.3/lib/python2.7/site-packages/hiredis-0.1.1-py2.7-linux-x86_64.egg
Processing dependencies for hiredis
Finished processing dependencies for hiredis

2.检查安装是否成功

--easy-install安装的扩展包默认在python的site-packages目录下

[root@njdyw ~]#whereis python2.7.3
python2.7: /bin/python2.7.3 /usr/local/python2.7.3
[root@njdyw ~]#cd /usr/local/python2.7.3/lib/python2.7/site-packages/
[root@njdyw site-packages]# ll

总计 408

-rw-r--r-- 1 root root  239 03-21 10:45 easy-install.pth
-rw-r--r-- 1 root root  119 03-21 10:07 README
-rw-r--r-- 1 root root 60401 03-21 10:45redis-2.7.2-py2.7.egg
-rw-r--r-- 1 root root 332125 03-21 10:12 setuptools-0.6c11-py2.7.egg
-rw-r--r-- 1 root root   30 03-21 10:12 setuptools.pth

可以看到redis-2.7.2-py2.7.egg包已经成功安装

3.测试连接

[root@njdyw site-packages]#python2.7.3
Python 2.7.3 (default, Mar 21 2013, 10:06:48)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>import redis
>>>redisClient=redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
>>> redisClient.set('test_redis','Hello Python')
True
>>> value=redisClient.get('test_redis')
>>> print value
Hello Python
>>> redisClient.delete('test_redis')
True
>>> value=redisClient.get('test_redis')
>>> print value
None
 
 
>>> dir(redis)
['AuthenticationError', 'Connection', 'ConnectionError', 'ConnectionPool', 'DataError', 'InvalidResponse', 'PubSubError', 'Redis', 'RedisError', 'ResponseError', 'StrictRedis', 'UnixDomainSocketConnection', 'VERSION', 'WatchError', '__all__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__version__', '_compat', 'client', 'connection', 'exceptions', 'from_url', 'utils']
>>> redisClient=redis.StrictRedis(host='127.0.0.1',port=6379,db=0)
>>> dir(redisClient)
['RESPONSE_CALLBACKS', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_zaggregate', 'append', 'bgrewriteaof', 'bgsave', 'bitcount', 'bitop', 'blpop', 'brpop', 'brpoplpush', 'client_kill', 'client_list', 'config_get', 'config_set', 'connection_pool', 'dbsize', 'debug_object', 'decr', 'delete', 'echo', 'eval', 'evalsha', 'execute_command', 'exists', 'expire', 'expireat', 'flushall', 'flushdb', 'from_url', 'get', 'getbit', 'getrange', 'getset', 'hdel', 'hexists', 'hget', 'hgetall', 'hincrby', 'hincrbyfloat', 'hkeys', 'hlen', 'hmget', 'hmset', 'hset', 'hsetnx', 'hvals', 'incr', 'incrbyfloat', 'info', 'keys', 'lastsave', 'lindex', 'linsert', 'llen', 'lock', 'lpop', 'lpush', 'lpushx', 'lrange', 'lrem', 'lset', 'ltrim', 'mget', 'move', 'mset', 'msetnx', 'object', 'parse_response', 'persist', 'pexpire', 'pexpireat', 'ping', 'pipeline', 'pttl', 'publish', 'pubsub', 'randomkey', 'register_script', 'rename', 'renamenx', 'response_callbacks', 'rpop', 'rpoplpush', 'rpush', 'rpushx', 'sadd', 'save', 'scard', 'script_exists', 'script_flush', 'script_kill', 'script_load', 'sdiff', 'sdiffstore', 'set', 'set_response_callback', 'setbit', 'setex', 'setnx', 'setrange', 'shutdown', 'sinter', 'sinterstore', 'sismember', 'slaveof', 'smembers', 'smove', 'sort', 'spop', 'srandmember', 'srem', 'strlen', 'substr', 'sunion', 'sunionstore', 'time', 'transaction', 'ttl', 'type', 'unwatch', 'watch', 'zadd', 'zcard', 'zcount', 'zincrby', 'zinterstore', 'zrange', 'zrangebyscore', 'zrank', 'zrem', 'zremrangebyrank', 'zremrangebyscore', 'zrevrange', 'zrevrangebyscore', 'zrevrank', 'zscore', 'zunionstore']
>>>

4.测试实例:

(1).把文本数据导入到redis

--导入的数据格式

[root@njdyw ~]#more data.txt
wolys # wolysopen111 # wolys@21cn.com
coralshanshan # 601601601 # zss1984@126.com
pengfeihuchao # woaidami # 294522652@qq.com
simulategirl # @#$9608125 # simulateboy@163.com
daisypp # 12345678 # zhoushigang_123@163.com
sirenxing424 # tfiloveyou # sirenxing424@126.com
raininglxy # 1901061139 # lixinyu23@qq.com
leochenlei # leichenlei # chenlei1201@gmail.com
z370433835 # lkp145566 # 370433835@qq.com

创建命令脚本

[root@njdyw ~]#cat imp_red.py
import redis
import re
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
r = redis.Redis(connection_pool=pool)
pipe = r.pipeline()
p=re.compile(r'(.*)\s#\s(.*)\s#\s(.*)');
pipe = r.pipeline()
f = open("data.txt")
matchs=p.findall(f.read())
for user in matchs:
  key='users_%s' %user[0].strip()
  pipe.hset(key,'pwd',user[1].strip()).hset(key,'email',user[2].strip())
pipe.execute()
f.close()

注意:要严格控制python脚本中的空格

--执行脚本

[root@njdyw ~]# python2.7.3 imp_red.py

查看导入数据

[root@njdyw ~]#redis-cli
redis 127.0.0.1:6379> keys *
 1) "users_xiaochuan2018"
 2) "users_coralshanshan"
 3) "users_xiazai200901"
 4) "users_daisypp"
 5) "users_boiny"
 6) "users_raininglxy"
 7) "users_fennal"
 8) "users_abc654468252"
 9) "users_babylovebooks"
10) "users_xl200811"
11) "users_baby19881018"
12) "users_darksoul0929"
13) "users_pengcfwxh"
14) "users_alex126126"
15) "users_jiongjiongmao"
16) "users_sirenxing424"
17) "users_mengjie007"
18) "users_cxx0409"
19) "users_candly8509"
20) "users_licaijun007"
21) "users_ai3Min2"
22) "users_bokil"
23) "users_z370433835"
24) "users_yiling1007"
25) "users_simulategirl"
26) "users_fxh852"
27) "users_baoautumn"
28) "users_huangdaqiao"
29) "users_q1718334567"
30) "users_xldq_l"
31) "users_beibeilong012"
32) "users_hudaoyin"
33) "users_yoyomika"
34) "users_jacksbalu"
35) "users_wolys"
36) "users_kangte1"
37) "users_demonhaodh"
38) "users_ysdz8"
39) "users_leochenlei"
40) "users_llx6888"
41) "users_pengfeihuchao"
redis 127.0.0.1:6379>
redis 127.0.0.1:6379>hget users_pengfeihuchao email
"294522652@qq.com"
redis 127.0.0.1:6379> hget users_llx6888 email
"linlixian200606@126.com"

好了,测试连接成功,如果你没有测试成功请认真看一下操作步骤

Python 相关文章推荐
sublime text 3配置使用python操作方法
Jun 11 Python
Python编程实现粒子群算法(PSO)详解
Nov 13 Python
Python实现确认字符串是否包含指定字符串的实例
May 02 Python
python 实时得到cpu和内存的使用情况方法
Jun 11 Python
详解Python中的正则表达式
Jul 08 Python
Python实现带下标索引的遍历操作示例
May 30 Python
Pandas之ReIndex重新索引的实现
Jun 25 Python
python Matplotlib底图中鼠标滑过显示隐藏内容的实例代码
Jul 31 Python
Python基础类继承重写实现原理解析
Apr 03 Python
Java多线程实现四种方式原理详解
Jun 02 Python
Django ORM判断查询结果是否为空,判断django中的orm为空实例
Jul 09 Python
Python3 用什么IDE开发工具比较好
Nov 28 Python
Python线程下使用锁的技巧分享
Sep 13 #Python
Python利用ORM控制MongoDB(MongoEngine)的步骤全纪录
Sep 13 #Python
python中字符串内置函数的用法总结
Sep 13 #Python
浅析python继承与多重继承
Sep 13 #Python
Python中分支语句与循环语句实例详解
Sep 13 #Python
Python爬虫小技巧之伪造随机的User-Agent
Sep 13 #Python
Python爬虫基础之XPath语法与lxml库的用法详解
Sep 13 #Python
You might like
php 归并排序 数组交集
2011/05/10 PHP
PHP笔记之:基于面向对象设计的详解
2013/05/14 PHP
PHP多维数组遍历方法(2种实现方法)
2015/12/10 PHP
php将html转为图片的实现方法
2017/05/19 PHP
WordPress 照片lightbox效果的运用几点
2009/06/22 Javascript
JavaScript的代码编写格式规范指南
2015/12/07 Javascript
完美实现bootstrap分页查询
2015/12/09 Javascript
EasyUI在表单提交之前进行验证的实例代码
2016/06/24 Javascript
AngularJS基础 ng-mouseenter 指令示例代码
2016/08/02 Javascript
js实现刷新页面后回到记录时滚动条的位置【两种方案可选】
2016/12/12 Javascript
鼠标拖动改变DIV等网页元素的大小的实现方法
2017/07/06 Javascript
canvas轨迹回放功能实现
2017/12/20 Javascript
js 实现watch监听数据变化的代码
2019/10/13 Javascript
vue组件创建的三种方式小结
2020/02/03 Javascript
js实现鼠标拖曳效果
2020/12/30 Javascript
[36:22]VP vs Serenity 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
基于python内置函数与匿名函数详解
2018/01/09 Python
Python Django2.0集成Celery4.1教程
2019/11/19 Python
Win10下用Anaconda安装TensorFlow(图文教程)
2020/06/18 Python
Python如何给函数库增加日志功能
2020/08/04 Python
Web前端绘制0.5像素的几种方法
2017/08/11 HTML / CSS
简单整理HTML5的基本特性和语法
2016/02/18 HTML / CSS
100%羊绒:NakedCashmere
2020/08/26 全球购物
PHP解析URL是哪个函数?怎么用?
2013/05/09 面试题
法院实习人员自我鉴定
2013/09/26 职场文书
英文版销售经理个人求职信
2013/11/20 职场文书
工商管理实习生自我鉴定范文
2013/12/18 职场文书
酒店七夕情人节活动策划方案
2014/08/24 职场文书
关于感恩的演讲稿200字
2014/08/26 职场文书
2015年平安创建工作总结
2015/04/29 职场文书
任命书格式模板
2015/09/22 职场文书
幼儿园托班开学寄语(2016春季)
2015/12/03 职场文书
PHP策略模式写法
2021/04/01 PHP
详解MySQL 用户权限管理
2021/04/20 MySQL
Python打包为exe详细教程
2021/05/18 Python
聊聊mysql都有哪几种分区方式
2022/04/13 MySQL