redis原理
Client:
import tab,redis
s = redis.Redis(host='10.39.3.110')
chan = s.pubsub()
chan.subscribe('fm100')
chan.parse_response()
Servre:
import redis
p = redis.Redis(host='10.39.3.110')
p.publish('fm100','wo ai beijing tiananmen')
1 #!/usr/bin/env python 2 #_*_ coding:utf-8 _*_ 3 4 import redis 5 6 class RedisHelper: 7 8 def __init__(self): 9 self.__conn = redis.Redis(host='10.39.3.110') 10 self.chan_sub = 'fm87.7' 11 self.chan_pub = 'fm104.5' 12 13 def get(self,key): 14 return self.__conn.get(key) 15 16 def set(self,key,value): 17 self.__conn.set(key,value) 18 19 def public(self,msg): 20 self.__conn.publish(self.chan_pub, msg) 21 return True 22 23 def subscribe(self): 24 pub = self.__conn.pubsub() 25 pub.subscribe(self.chan_sub) 26 pub.parse_response() 27 return pub 28 29 if __name__ == '__main__': 30 t = RedisHelper() 31 t.public('test')