上一篇文章( 地址)
1、Python 操作 Window 端 Redis
具体步骤:
- 第一步:安装Redis扩展(在命令行窗口运行)
pip install redis
- 第二步:连接(在 pycharm 里运行)
r = redis.StrictRedis(host='localhost',port=6379,db=0)
2、Python 操作 Linux 端 Redis
具体步骤:
- 第一步:Redis 开启远程访问
vim redis-6.2.1/redis.conf
- (将 bind 127.0.0.1注释)
- (protected-mode 改为no)
- (daemonize 改为yes)
- 第二步:将 Linux 防火墙关掉
systemctl stop firewalld.service
(关闭防火墙命令)systemctl start firewalld.service
(开启防火墙)systemctl disable firewalld.service
(关闭开机自启动)systemctl enable firewalld.service
(开启开机启动)- 第三步:查询 Linux 客户端的 ipv4
ifconfig
Python 端命令如下:
import redis
# 此处的 host 后面的为主机 ipv4(用 ifconfig 命令查看)
r = redis.StrictRedis(host="192.168.56.130", port=6379, password="")
r.set("name", "susu")
r.set('age', '19')
age = r.get('age').decode("utf-8")
print(age)
# output:19
Linux 端命令:mset name susu age 19