一、安装服务器
Q: redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused.
A: redis 安装redis服务
sudo apt-get install redis-server
二、防火墙关闭
Q:redis exceptions ConnectionError Error 113 connecting ...... no route to host
A: 关闭防火墙
sudo iptables -F
但是关闭之后,还是会出现no route to host 的问题,查询进程,发现用kill的方式杀不掉进程(会自动重启 /usr/bin/redis-server 127.0.0.1:6379)
ps -ef | grep -i redis #查看redis进程id
#杀死进程
sudo kill -9 id
关闭redis服务
/etc/init.d/redis-server stop
重启redis:
redis-server
三、修改配置文件
重启redis后,redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients.
找到配置文件 /etc/redis/redis-conf
1. 将绑定的ip号注释
#bind 127.0.0.1
2. 在配置文件最后加上
protected-mode no
重启后顺利连接
附:
发送程序:
import time
import redis
r = redis.StrictRedis(host = "10.11.80.200", port = 6379, db=1)
#r = redis.StrictRedis(host = "127.0.0.1", port = 6379, db=1)
i = 0
while True:
i += 1
r.publish("first channel", "the i is " + str(i))
print("the i is " + str(i))
time.sleep(1)
接收程序:
import time
import redis
r = redis.StrictRedis(host = "10.11.80.200", port = 6379, db=1)
p = r.pubsub()
p.subscribe("first channel")
for item in p.listen(): #监听状态:有消息发布了就拿过来
if item['type'] == 'message':
print(item['data'])