Python操作Redis服务器

2016-12-16 15:07:00
村里来的扫地僧
原创
1700

下载redis程序:


[root@linux-02 opt]# wget https://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz --no-check-certificate
--2016-12-03 19:13:10--  https://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz
Resolving pypi.python.org... 151.101.16.223, 2a04:4e42:4::223
Connecting to pypi.python.org|151.101.16.223|:443... connected.
WARNING: certificate common name “www.python.org” doesn’t match requested host name “pypi.python.org”.
HTTP request sent, awaiting response... 200 OK
Length: 62204 (61K) [application/octet-stream]
Saving to: “redis-2.9.1.tar.gz”

100%[===========================================================================================>] 62,204      16.1K/s   in 3.8s    

2016-12-03 19:13:16 (16.1 KB/s) - “redis-2.9.1.tar.gz” saved [62204/62204]
解压缩文件:



[root@linux-02 opt]# tar xf redis-2.9.1.tar.gz 
[root@linux-02 opt]# cd redis-2.9.1
[root@linux-02 redis-2.9.1]# ls
CHANGES  INSTALL  LICENSE  MANIFEST.in  PKG-INFO  README.rst  redis  redis.egg-info  setup.cfg  setup.py  tests
编译安装程序:



[root@linux-02 redis-2.9.1]# python setup.py install
running install
running bdist_egg
running egg_info
writing redis.egg-info/PKG-INFO
writing top-level names to redis.egg-info/top_level.txt
writing dependency_links to redis.egg-info/dependency_links.txt
reading manifest file 'redis.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no previously-included files found matching '__pycache__'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
writing manifest file 'redis.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_py
creating build
creating build/lib
creating build/lib/redis
copying redis/utils.py -> build/lib/redis
copying redis/__init__.py -> build/lib/redis
copying redis/client.py -> build/lib/redis
copying redis/connection.py -> build/lib/redis
copying redis/exceptions.py -> build/lib/redis
copying redis/_compat.py -> build/lib/redis
copying redis/sentinel.py -> build/lib/redis
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
creating build/bdist.linux-x86_64/egg/redis
copying build/lib/redis/utils.py -> build/bdist.linux-x86_64/egg/redis
copying build/lib/redis/__init__.py -> build/bdist.linux-x86_64/egg/redis
copying build/lib/redis/client.py -> build/bdist.linux-x86_64/egg/redis
copying build/lib/redis/connection.py -> build/bdist.linux-x86_64/egg/redis
copying build/lib/redis/exceptions.py -> build/bdist.linux-x86_64/egg/redis
copying build/lib/redis/_compat.py -> build/bdist.linux-x86_64/egg/redis
copying build/lib/redis/sentinel.py -> build/bdist.linux-x86_64/egg/redis
byte-compiling build/bdist.linux-x86_64/egg/redis/utils.py to utils.pyc
byte-compiling build/bdist.linux-x86_64/egg/redis/__init__.py to __init__.pyc
byte-compiling build/bdist.linux-x86_64/egg/redis/client.py to client.pyc
byte-compiling build/bdist.linux-x86_64/egg/redis/connection.py to connection.pyc
byte-compiling build/bdist.linux-x86_64/egg/redis/exceptions.py to exceptions.pyc
byte-compiling build/bdist.linux-x86_64/egg/redis/_compat.py to _compat.pyc
byte-compiling build/bdist.linux-x86_64/egg/redis/sentinel.py to sentinel.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying redis.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying redis.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying redis.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying redis.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist/redis-2.9.1-py2.6.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing redis-2.9.1-py2.6.egg
creating /usr/lib/python2.6/site-packages/redis-2.9.1-py2.6.egg
Extracting redis-2.9.1-py2.6.egg to /usr/lib/python2.6/site-packages
Adding redis 2.9.1 to easy-install.pth file

Installed /usr/lib/python2.6/site-packages/redis-2.9.1-py2.6.egg
Processing dependencies for redis==2.9.1
Finished processing dependencies for redis==2.9.1
[root@linux-02 redis-2.9.1]# 
使用命令行操作Redis:



import redis
r = redis.Redis(host='127.0.0.1',port=6379,password='password123@',db=0)
r.set('name','key')
r.get('name')
r.dbsize()
r.keys()
exit()
输出结果:



>>> import redis
>>> r = redis.Redis(host='127.0.0.1',port=6379,password='password123@',db=0)
>>> r.set('name','key')
True
>>> r.get('name')
'key'
>>> r.dbsize()
7L
>>> r.keys()
['user002', 'k3', 'name', 'user001', 'k1', 'KEY3', 'k2']
>>> 
发表评论
贰 减 柒 =
评论通过审核后显示。