Connecting to Redis with Python
There are a couple of Python clients for connecting to Redis. In this recipe, we will briefly introduce how to use the Python Redis client, redis-py
.
Getting ready…
You need to finish the installation of the Redis server as we described in the Downloading and installing Redis recipe in Chapter 1, Getting Started with Redis.
You need to have Python 2.6+ or 3.4+ installed.
How to do it...
To show how to connect to Redis with Python, first of all, we need to install the redis-py
library. With the help of PyPI, redis-py
can be easily installed by running:
pip install redis
Connecting to the Redis Server
Now let's create RedisDemo.py
with the following code:
The demo application will print the following lines:
Address for Extreme Pizza is 300 Broadway, New York, NY 10011
Favorite Restaurants: [Olive Garden, PF Chang's, Olive Garden, PF Chang's, Indian Tandoor, Longhorn Steakhouse, Outback Steakhouse, Red Lobster, Outback Steakhouse, Red Lobster]
Using pipelines
Redis pipelines...