Python connection to Redis connection Configuration

Source: Internet
Author: User

System Environment: OS: Oracle Linux Enterprise 5.6 redis: redis-2.6.8python: Python-2.7.3redis python package version: redis-2.7.2.tar prerequisites: 1. make sure that Redis is successfully installed and configured correctly. Refer to document 2. make sure that the Python environment has been successfully configured. Refer to the documentation to configure python connection to redis: 1. install the Python package of Redis: use easy-install to install. For the configuration of easy-install, refer to the above Python environment setup. [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 easy_install-8FAlft/setup. py-q bdist_egg -- dist-dir/tmp/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 -- install Parser package (optional) Description: parser can control how to parse the redis response content. Redis-py contains two Parser classes: PythonParser and HiredisParser. By default, if the hiredis module is installed, redis-py will use HiredisParser; otherwise, PythonParser will be used. HiredisParser is written in C and maintained by the redis core team. Its performance is more than 10 times higher than that of PythonParser. Therefore, it is recommended to use HiredisParser. Install 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. check whether the installation is successful -- the easy-install extension package is installed in the site-packages directory of python by default [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 total 408-rw-r -- r -- 1 root 239 03-21 easy-install.pth-rw-r -- r -- 1 root 119 03-21 README-rw-r -- 1 root 60401 03-21 10: 45redis-2.7.2-py2.7.egg-rw-r -- r -- 1 root 332125 03-21 setuptools-0.6c11-py2.7.egg-rw-r -- r -- 1 root 30 03-21 setuptools. pth you can see that the redis-2.7.2-py2.7.egg package has After successful installation 3. test connection [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 = '2017. 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 valueNone >>> dir (redis) ['authenticationerror', 'connection', 'connectionerror', 'connectionpool ', 'dataererror', 'invalidresponse', 'pubsuberror ', 'redis', 'rediserror', 'responseerror', 'strictredis ', 'unixdomainsocketconnection', 'version', 'watchererror ', '_ all _', '_ builtins _', '_ doc _', '_ fi Le _ ',' _ loader _ ',' _ name _ ',' _ package _ ',' _ path __', '_ version _', '_ compat', 'client', 'connection', 'exception', 'From _ url ', 'utils'] >>> redisClient = redis. strictRedis (host = '2017. 0.0.1 ', port = 6379, db = 0) >>> dir (redisClient) ['response _ callbacks',' _ class __', '_ ins INS _', '_ delattr _', '_ delitem _', '_ dict _', '_ doc __', '_ format _', '_ getattribute _', '_ getitem _', '_ hash _', '_ I Nit _ ',' _ module _ ',' _ new _ ',' _ reduce _ ',' _ performance_ex __', '_ repr _', '_ setattr _', '_ setitem _', '_ sizeof _', '_ str __', '_ subclasshook _', '_ weakref _', '_ zaggregate', 'append', 'bgrewriteaof ', 'bgsave', 'bitcount ', 'bitop', 'blpop', 'brpop', 'brplpush', 'client _ kill ', 'client _ list', 'config _ get', 'config _ set ', 'Connection _ pool ', 'dbsize', 'debug _ object', 'desc', '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', 'hmet ', 'hmset', 'hset', 'hsetnx', 'hvals', 'inc', 'encrbyfloat', 'info ', 'keys ', 'lastsave', 'lindex', 'linsert ', 'llen', 'lock', 'lpop', 'lptu', 'lpushhx', 'lrange ', 'lrem ', 'Lset', 'ltrim', 'mget', 'Move ', 'mset', 'msetnx', 'object', 'parse _ response', 'persist ', 'pexpire ', 'pexpireat', 'ping', 'pipeline', 'pttl ', 'Publish', 'pubsub ', 'randkey', 'register _ script ', 'rename', 'renamenx', 'response _ callbacks', 'rpop ', 'rpoplpush', 'rptu', 'rpushx', 'sadd', 'save ', 'scard ', 'script _ exists', 'script _ flush', 'script _ kill ', 'script _ load', 'sdid', '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 ', 'zencrby', 'zinterstore', 'zrange', 'zrangebyscore ', 'zrank', 'zrem ', 'zremrangebyra Nk ', 'zremrangebyscore', 'zrevrange', 'zrevrangebyscore ', 'zrevrank', 'zscore ', 'zunionstore'] >>> 4. test instance: (1 ). import text data to redis-the imported data format [root @ njdyw ~] # More data.txt wolys # wolysopen111 # wolys@21cn.comcoralshanshan #601601601 # zss1984@126.compengfeihuchao # woaidami # 294522652@qq.comsimulategirl #$9608125 # simulateboy@163.comdaisypp #12345678 # zhoushigang_123@163.comsirenxing424 # tfiloveyou # sirenxing424@126.comraininglxy #1901061139 # lixinyu23@qq.comleochenlei # leichenlei # chenlei1201@gmail.comz370433835 # lkp145566 #370433835 @ q Q.com -- CREATE Command Script [root @ njdyw ~] # Cat imp_red.pyimport redisimport repool = redis. connectionPool (host = '2017. 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 () Route cute () f. close () Note: Strictly control spaces in the python script-execute the script [root @ njdyw ~] # Python2.7.3 imp_red.py -- view the imported data [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) "regular" 9) "users_babylovebooks" 10) "users_xl200811" 11) "users_baby19881018" 12) "regular" 13) "regular" 14) "users_alex126126" 15) "regular" 16) "regular" 17) "users_mengjie007" 18) "users_cxx0409" 19) "users_candly8509" 20) "users_licaijun007" 21) "users_ai3Min2" 22) "users_bokil" 23) "regular" 24) "users_yiling1007" 25) "users_simulategirl" 26) "users_fxh852" 27) "regular" 28) "users_huangdaqiao" 29) "concurrent" 30) "login" 31) "login" 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) "comment" redis 127.0.0.1: 6379> redis 127.0.0.1: 6379> hget guest email "294522652@qq.com" redis 127.0.0.1: 6379> hget users_llx6888 email "linlixian200606@126.com"

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.