redis connecter
vim redisPubSub.py
用来连接redis server并封装了发布与订阅的功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import redis
class RedisPubSubHelper():
def __init__(self): self.__conn = redis.Redis(host='123.57.233.243')
def publish(self, message, channel): self.__conn.publish(channel, message) return True
def subscribe(self, channel): pub = self.__conn.pubsub() pub.subscribe(channel) pub.parse_response() return pub
|
listen 2 redis server
vim listen2Redis.py
1 2 3 4 5 6 7 8
| import redisPubSub
r = redisPubSub.RedisPubSubHelper()
data = r.subscribe('fm155.9')
print(data.parse_response())
|
insert data 2 redis server
vim insert2Redis.py
1 2 3 4 5 6
| import redisPubSub
r = redisPubSub.RedisPubSubHelper()
r.publish('PolarSnow', 'fm155.9')
|
demo
依次启动
- python listen2Redis.py
- python insert2Redis.py
listen2Redis.py 的回显
1
| [b'message', b'fm155.9', b'PolarSnow']
|