LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > Linux教程 >

Python连接Redis

时间:2013-06-25 21:34来源:wubiaoblog.com/archives/606 编辑:吴飚 点击:
1、安装Redis的Python包 使用easy-install安装,关于easy-install的配置,参考: https://linuxeye.com/Linux/1789.html [root@biao ~]# easy_install2.7.3 redisSearching for redisReading http://pypi.python.org/simple/redis
1、安装Redis的Python包
使用easy-install安装,关于easy-install的配置,参考:https://linuxeye.com/Linux/1789.html
[root@biao ~]# 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-Y85sxR/redis-2.7.2/egg-dist-tmp-z3jKjB
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

可以在相应目录下检查是否安装成功,easy-install安装的扩展包默认在python的site-packages目录下:

[root@biao ~]# whereis python2.7.3
python2.7: /bin/python2.7.3 /usr/local/python2.7.3
[root@biao ~]# cd /usr/local/python2.7.3/lib/python2.7/site-packages/
[root@biao site-packages]# ll
total 620
-rwxrwxrwx 1 root root    310 Jan 10 01:34 easy-install.pth
drwxrwxr-x 2 grid grid   4096 Dec 11 21:09 hbase
drwxrwxrwx 4 root root   4096 Dec  5 19:05 numpy-1.6.2-py2.7-linux-i686.egg
drwxr-xr-x 2 1000 1000   4096 Nov 28  2011 python-memcached-1.48
-rwxr-xr-x 1 root root  40448 Dec 29 00:13 python-memcached-1.48.tar.gz
-rwxrwxrwx 1 root root    119 Dec  5 18:02 README
-rw-r--r-- 1 root root  60370 Jan 10 01:34 redis-2.7.2-py2.7.egg
-rwxrwxrwx 1 root root 331999 Dec  5 18:29 setuptools-0.6c11-py2.7.egg
-rwxrwxrwx 1 root root     30 Dec  5 18:29 setuptools.pth
-rw-rw-r-- 1 grid grid 156642 Dec 11 20:57 thrift-0.9.0-py2.7-linux-i686.egg
OK,redis-2.7.2-py2.7.egg就是我们需要的。

2、测试
root@biao ~]# python2.7.3 
Python 2.7.3 (default, Dec  5 2012, 17:57:43) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] 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
>>>
如上所示,Python连接Redis成本,能正常操作。

Python就是简单,方便简洁。更多方法以后再试
>>> 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']
关于更多redis-Python详细介绍,可以参考作者的提供的资料:https://github.com/andymccurdy/redis-py

转载请保留固定链接: https://linuxeye.com/Linux/1790.html

------分隔线----------------------------
标签:PythonRedis
栏目列表
推荐内容