标签:


wget https://pypi.python.org/packages/source/r/redis/redis-2.9.1.tar.gz tar zxfredis-2.9.1.tar.gz cdredis-2.9.1 python setup.pyinstall



Redis订阅发布功能
import redis
class RedisHelper:
def __init__(self):
self.__conn = redis.Redis(host=‘127.0.0.1‘)
self.chan_sub = ‘fm87.7‘
def subscribe(self):
pub = self.__conn.pubsub()
pub.subscribe(self.chan_sub)
pub.parse_response()
return pub
if __name__ == ‘__main__‘:
r = RedisHelper()
recv = r.subscribe() #订阅fm87.7频道
while True: #循环监听接收
print recv.parse_response() #监听该频道,并接收客户端的消息
client端:
import redis
class RedisHelper:
def __init__(self):
self.__conn = redis.Redis(host=‘127.0.0.1‘)
self.chan_pub = ‘fm87.7‘
def public(self,msg): #发布内容到fm87.7频道
self.__conn.publish(self.chan_pub, msg)
return True
if __name__ == ‘__main__‘:
t = RedisHelper()
t.public(‘test‘) #test为发布的内容
server订阅fm87.7的消息
执行python client.py 发布消息后,server得到订阅的消息

标签:
原文地址:http://www.cnblogs.com/yangmv/p/5198915.html