python和redis简单交互
1.安装redis模块
pip3 install redis
2.redis模块简单使用:
# /usr/bin/env python3
import redis
class Myredis(object):
def __init__(self, host, port=6379):
try:
self.conn = redis.StrictRedis(host=host, port=port)
except Exception as e:
print(e)
else:
print('连接成功')
def add(self, key, value):
result = self.conn.set(key, value)
print(result)
def get(self, key):
result = self.conn.get(key)
print(result)
def mod(self, key, value):
result = self.conn.get(key)
if result:
result2 = self.conn.set(key, value)
print(result2)
else:
print("key " + key + "is not found")
def rem(self,key):
result = self.conn.delete(key)
if result != 0:
print('删除成功')
if __name__ == "__main__":
myredis = Myredis('192.168.26.131')
myredis.add('name','wanghui')
myredis.mod('name','baby')
myredis.get('name')
myredis.rem('name')