@zengxiankui
2017-08-04T13:18:33.000000Z
字数 1148
阅读 1410
Python
#!/usr/bin/python
# -*- coding: utf-8 -*-
#File: publish.py 信息发布者;
import sys, redis;
reload(sys);
sys.setdefaultencoding('utf8');
###连接池方式OK
pool = redis.ConnectionPool(host = "10.0.30.91", port = 6379, password = "hsb_redis_123", db = 0);
r = redis.StrictRedis(connection_pool = pool);
###连接池方式OK
#r = redis.StrictRedis(host = "10.0.30.91", port = 6379, password = "hsb_redis_123", db = 0);
while(True):
input = raw_input("publish>>> ");
r.publish("ch2", input);
if("exit" == input):
print("停止发布");
break;
else:pass;
else:pass;
print("EXIT");
#!/usr/bin/python
# -*- coding: utf-8 -*-
#File: subscribe.py 信息订阅者;
import sys, redis;
reload(sys);
sys.setdefaultencoding('utf8');
###连接池方式OK
pool = redis.ConnectionPool(host = "10.0.30.91", port = 6379, password = "hsb_redis_123", db = 0);
r = redis.StrictRedis(connection_pool = pool);
###单链接方式OK
#r = redis.StrictRedis(host = "10.0.30.91", port = 6379, password = "hsb_redis_123", db = 0);
p = r.pubsub();
p.subscribe("ch2");
for item in p.listen():
print(item);
if(item["type"] == "subscribe"):
print("Subscribe OK!");
continue;
else:pass;
if(item["type"] == "message"):
data = item["data"];
print(data);
if("exit" == data):
break;
else:pass;
else:pass;
else:pass;
p.unsubscribe("ch2");
print("EXIT");