Python Connection Redis Connection configuration

Source: Internet
Author: User
System environment:

Os:oracle Linux Enterprise 5.6

redis:redis-2.6.8

python:python-2.7.3

Python package version of Redis: Redis-2.7.2.tar


Prerequisites:

1. Ensure that Redis has been successfully installed and configured correctly, refer to the documentation

2. Ensure that the Python environment is configured successfully, refer to the documentation

To configure Python to connect to Redis:

1. Install the Redis python package:

Use Easy-install installation, about Easy-install configuration, refer to the above Python environment to build.

[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


--Install the parser package (optional)

Description: Parser can control how the contents of a Redis response are parsed. Redis-py contains two parser classes, Pythonparser and Hiredisparser. By default, if the Hiredis module is already installed, Redis-py will use Hiredisparser, otherwise pythonparser will be used.

Hiredisparser is written in C, and is maintained by the Redis core team, with performance up to 10 times times higher than pythonparser, so it is recommended. Installation method, using 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. Check if the installation is successful

--easy-install installed expansion pack default in Python's Site-packages directory

[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

Total 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 03-21 10:12 setuptools.pth


You can see that the Redis-2.7.2-py2.7.egg package has been successfully installed


3. Test the connection

[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 ', ' Hset ' NX ', ' hvals ', ' incr ', ' incrbyfloat ', ' info ', ' Keys ', ' lastsave ', ' lindex ', ' linsert ', ' llen ', ' lock ', ' lpop ', ' Lpush ', ' LP ' Ushx ', ' lrange ', ' lrem ', ' LSet ', ' LTrim ', ' mget ', ' mOve ', ' mset ', ' msetnx ', ' object ', ' parse_response ', ' persist ', ' pexpire ', ' pexpireat ', ' ping ', ' Pipeline ', ' Pttl ', ' publi ' Sh ', ' 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 ', ' su Nion ', ' sunionstore ', ' time ', ' Transaction ', ' TTL ', ' type ', ' unwatch ', ' watch ', ' zadd ', ' zcard ', ' zcount ', ' Zincrby ', ' Zin ' Terstore ', ' zrange ', ' zrangebyscore ', ' Zrank ', ' zrem ', ' Zremrangebyrank ', ' zremrangebyscore ', ' zrevrange ', ' Zrevrangebyscore ', ' Zrevrank ', ' zscore ', ' Zunionstore ']

>>>


4. Test Example:

(1). Import text data into Redis

--The imported data format

[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


--Create a command script

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


Note: To tightly control space in a Python script

--Execute script

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


--View Import data

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

"Users_xl200811"

One) "users_baby19881018"

"users_darksoul0929"

"Users_pengcfwxh"

"Users_alex126126"

() "Users_jiongjiongmao"

() "users_sirenxing424"

) "Users_mengjie007"

) "users_cxx0409"

"Users_candly8509"

"Users_licaijun007"

) "Users_ai3min2"

"Users_bokil"

) "users_z370433835"

"Users_yiling1007"

"Users_simulategirl"

() "users_fxh852"

"Users_baoautumn"

) "Users_huangdaqiao"

) "users_q1718334567"

() "users_xldq_l"

) "users_beibeilong012"

"Users_hudaoyin"

) "Users_yoyomika"

"Users_jacksbalu"

"Users_wolys"

"Users_kangte1"

(PNS) "Users_demonhaodh"

"Users_ysdz8"

"Users_leochenlei"

(+) "users_llx6888"

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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.