1、安装redis库
pip install redis
2、编写示例代码
import redis
#redis服务器IP
host='192.168.2.142'
#redis端口
port=6379
#redis密码
password='123456'
# 建立Redis连接
r = redis.Redis(host=host, port=port, password=password)
# 执行KEYS *命令获取所有键名,并打印
keys = r.keys('*')
for key in keys:
print(key.decode('utf-8'))
# 设置键值对
r.set('key', 'value')
# 获取值
value = r.get('key')
print(value)
# 删除键值对
r.delete('key')
# 判断键是否存在
exists = r.exists('key')
print(exists)