Python learns to use Python to manipulate the Redis database

Source: Internet
Author: User
Tags python script install redis

Recently writing a script to check the size of a Redis key value in all of the game area service profiles on a single server was intended to solve this problem by using shell+awk+sed, but because Redis configuration information is in the form of PHP arrays. The shell script was not written in the moment, Ask someone to help write a Python script, but their python is not very proficient, so according to the Python knowledge involved in the script is now learning to use, and then according to their own needs to change the script. Here's how to use Python to manipulate the Redis database.


The Python drive source for Redis is https://github.com/andymccurdy/redis-py

It is recommended to use Python2.5 above, the default Python version is 2.4 under CentOS 5.x, and the default Python version is 2.6 under CentOS 6.x.


A python driver that installs Redis

CentOS 6.x

sudo pip install Redis (recommended) or sudo easy_install Redis

The Python driver installed on the CentOS 5.x requires the installation of Python2.5 or more, and it is recommended to install Python2.6

wget https://github.com/andymccurdy/redis-py/archive/master.zipunzip redis-py-master.zip CD Redis-py-mastersudo python2.6 setup.py Install


Two using the Redis module

The Debug Python statement recommends the use of Ipython, an enhanced Python terminal that can be installed using the Yum install Ipython

In [Redisin]: import: R=redis. Strictredis (host= ' localhost ', port=6380,db=3) in [all]: R.set (' Test ', ' test123 ') out[90]: Truein []: R.get (' Test ') out[ Test123 ' in [9]: R.delete ("Test") out[9]: 1In [+]: R.config_get ("MaxMemory") out[41]: {' maxmemory ': ' 0 '}in [46]: R.config_set ("timeout", 1) out[46]: Truein [+]: R.config_get ("timeout") out[47]: {' timeout ': ' 1 '}


Use the Strictredis class of the Redis module to operate on Redis. Most operations are the same as the official Redis command syntax, but some commands use exceptions.

1) Redis-py did not introduce the Select command for security reasons

2) redis-py Use delete instead of DEL command

3) redis-py use Config_get and config_set instead of config get and config set




This article is from the Linux SA John blog, so be sure to keep this source http://john88wang.blog.51cto.com/2165294/1423693

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.