1
2# step 1: import the redis-py client package
3import redis
4
5# step 2: define our connection information for Redis
6# Replaces with your configuration information
7redis_host = "localhost"
8redis_port = 6379
9redis_password = ""
10
11
12def hello_redis():
13 """Example Hello Redis Program"""
14
15 # step 3: create the Redis Connection object
16 try:
17
18 # The decode_repsonses flag here directs the client to convert the responses from Redis into Python strings
19 # using the default encoding utf-8. This is client specific.
20 r = redis.StrictRedis(host=redis_host, port=redis_port, password=redis_password, decode_responses=True)
21
22 # step 4: Set the hello message in Redis
23 r.set("msg:hello", "Hello Redis!!!")
24
25 # step 5: Retrieve the hello message from Redis
26 msg = r.get("msg:hello")
27 print(msg)
28
29 except Exception as e:
30 print(e)
31
32
33if __name__ == '__main__':
34 hello_redis()