最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python連接Redis的基本配置方法

 更新時(shí)間:2018年09月13日 17:39:25   投稿:wdc  
本文分享了Linux系統(tǒng)下Python連接Redis的基本配置方法,Python連接Redis需要Redis的Python包支持

在Linux系統(tǒng)下Python連接Redis的基本配置方法具體操作步驟

系統(tǒng)環(huán)境:

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已成功安裝并且正確配置,參考文檔

主從配置文檔:

//m.fzitv.net/article/147397.htm

2.確保Python環(huán)境已成功配置,參考文檔

http://m.fzitv.net/article/109765.htm

配置python連接redis:

1.安裝Redis的Python包:

使用easy-install安裝,關(guān)于easy-install的配置,參考以上Python環(huán)境的搭建。

[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包(可選)

說(shuō)明:Parser可以控制如何解析redis響應(yīng)的內(nèi)容。redis-py包含兩個(gè)Parser類(lèi),PythonParser和HiredisParser。默認(rèn),如果已經(jīng)安裝了hiredis模塊,redis-py會(huì)使用HiredisParser,否則會(huì)使用PythonParser。

HiredisParser是C編寫(xiě)的,由redis核心團(tuán)隊(duì)維護(hù),性能要比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安裝的擴(kuò)展包默認(rèn)在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

總計(jì) 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包已經(jīng)成功安裝

3.測(cè)試連接

[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.測(cè)試實(shí)例:

(1).把文本數(shù)據(jù)導(dǎo)入到redis

--導(dǎo)入的數(shù)據(jù)格式

[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

創(chuàng)建命令腳本

[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()

注意:要嚴(yán)格控制python腳本中的空格

--執(zhí)行腳本

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

查看導(dǎo)入數(shù)據(jù)

[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"

好了,測(cè)試連接成功,如果你沒(méi)有測(cè)試成功請(qǐng)認(rèn)真看一下操作步驟

相關(guān)文章

  • PyTorch中torch.utils.data.DataLoader實(shí)例詳解

    PyTorch中torch.utils.data.DataLoader實(shí)例詳解

    torch.utils.data.DataLoader主要是對(duì)數(shù)據(jù)進(jìn)行batch的劃分,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.utils.data.DataLoader的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Python解析命令行讀取參數(shù)之a(chǎn)rgparse模塊

    Python解析命令行讀取參數(shù)之a(chǎn)rgparse模塊

    這篇文章主要介紹了Python解析命令行讀取參數(shù)之a(chǎn)rgparse模塊,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • django獲取from表單multiple-select的value和id的方法

    django獲取from表單multiple-select的value和id的方法

    今天小編就為大家分享一篇django獲取from表單multiple-select的value和id的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • python計(jì)算日期之間的放假日期

    python計(jì)算日期之間的放假日期

    這篇文章主要為大家詳細(xì)介紹了python計(jì)算日期之間的放假日期,實(shí)現(xiàn)自動(dòng)查詢節(jié)日,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Python中的Logging模塊在項(xiàng)目應(yīng)用小結(jié)

    Python中的Logging模塊在項(xiàng)目應(yīng)用小結(jié)

    Python標(biāo)準(zhǔn)庫(kù)中的logging模塊為我們提供了強(qiáng)大而靈活的日志記錄功能,本文將介紹如何使用logging模塊,并探討其在Python開(kāi)發(fā)中的實(shí)際應(yīng)用,感興趣的朋友一起看看吧
    2024-04-04
  • python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例

    python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例

    今天小編就為大家分享一篇python paramiko利用sftp上傳目錄到遠(yuǎn)程的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • python第三方模塊xmltodict庫(kù)優(yōu)雅處理xml格式為json

    python第三方模塊xmltodict庫(kù)優(yōu)雅處理xml格式為json

    這篇文章主要為大家介紹了python第三方模塊xmltodict庫(kù)優(yōu)雅處理xml格式為json實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • 分享一個(gè)可以生成各種進(jìn)制格式IP的小工具實(shí)例代碼

    分享一個(gè)可以生成各種進(jìn)制格式IP的小工具實(shí)例代碼

    這篇文章主要給大家分享了一個(gè)可以生成各種進(jìn)制格式IP的小工具,利用的語(yǔ)言是python實(shí)現(xiàn)的一個(gè)小工具,這個(gè)小工具對(duì)大家的日常使用與開(kāi)發(fā)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起看看吧。
    2017-07-07
  • Python單元測(cè)試框架unittest使用方法講解

    Python單元測(cè)試框架unittest使用方法講解

    這篇文章主要介紹了Python單元測(cè)試框架unittest使用方法講解,本文講解了unittest概述、命令行接口、測(cè)試案例自動(dòng)搜索、創(chuàng)建測(cè)試代碼、構(gòu)建測(cè)試套件方法等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • Python之list對(duì)應(yīng)元素求和的方法

    Python之list對(duì)應(yīng)元素求和的方法

    今天小編就為大家分享一篇Python之list對(duì)應(yīng)元素求和的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06

最新評(píng)論

中方县| 罗山县| 桂平市| 琼中| 南开区| 同仁县| 杨浦区| 奈曼旗| 临猗县| 香港| 达孜县| 宜都市| 江源县| 泰顺县| 南宁市| 临西县| 延庆县| 淮阳县| 渭南市| 金坛市| 阿鲁科尔沁旗| 奉贤区| 滦南县| 江门市| 澄迈县| 诏安县| 乌拉特后旗| 托克逊县| 长泰县| 石屏县| 尚义县| 和硕县| 双鸭山市| 高州市| 嘉义市| 庐江县| 蒲城县| 浦江县| 宁阳县| 开封市| 昌黎县|