侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python连接Redis的基本配置方法

Python  /  管理员 发布于 7年前   231

在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已成功安装并且正确配置,参考文档

主从配置文档:

///article/147397.htm

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

https:///article/109765.htm

配置python连接redis:

1.安装Redis的Python包:

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

[root@njdyw bin]# easy_install2.7.3 redisSearching for redisReading http://pypi.python.org/simple/redis/Reading http://github.com/andymccurdy/redis-pyBest match: redis 2.7.2Downloading http://pypi.python.org/packages/source/r/redis/redis-2.7.2.tar.gz#md5=17ac60dcf13eb33f82cc25974ab17157Processing redis-2.7.2.tar.gzRunning redis-2.7.2/setup.py -q bdist_egg --dist-dir /tmp/easy_install-8FAlft/redis-2.7.2/egg-dist-tmp-JzQViJzip_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.eggProcessing dependencies for redisFinished 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 hiredisSearching for hiredisReading http://pypi.python.org/simple/hiredis/Reading https://github.com/pietern/hiredis-pyBest match: hiredis 0.1.1Downloading http://pypi.python.org/packages/source/h/hiredis/hiredis-0.1.1.tar.gz#md5=92128474f6fb027cfb8587fce724ea8eProcessing hiredis-0.1.1.tar.gzRunning hiredis-0.1.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-ZanSCB/hiredis-0.1.1/egg-dist-tmp-XCZBQ0zip_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.eggProcessing dependencies for hiredisFinished processing dependencies for hiredis

2.检查安装是否成功

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

[root@njdyw ~]#whereis python2.7.3python2.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.3Python 2.7.3 (default, Mar 21 2013, 10:06:48)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2Type "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 valueHello Python>>> redisClient.delete('test_redis')True>>> value=redisClient.get('test_redis')>>> print valueNone  >>> 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.txtwolys # wolysopen111 # [email protected] # 601601601 # [email protected] # woaidami # [email protected] # @#$9608125 # [email protected] # 12345678 # [email protected] # tfiloveyou # [email protected] # 1901061139 # [email protected] # leichenlei # [email protected] # lkp145566 # [email protected]

创建命令脚本

[root@njdyw ~]#cat imp_red.pyimport redisimport repool = 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-cliredis 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"[email protected]"redis 127.0.0.1:6379> hget users_llx6888 email"[email protected]"

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


  • 上一条:
    Python函数any()和all()的用法及区别介绍
    下一条:
    Python线程下使用锁的技巧分享
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客