设为首页 收藏本站
查看: 713|回复: 0

[经验分享] 【Python之旅】第七篇(三):使用Redis订阅服务

[复制链接]

尚未签到

发表于 2018-11-6 08:16:49 | 显示全部楼层 |阅读模式
  在C/S架构中,可以充分地利用Redis订阅服务,实现服务器端和客户端的信息收发,下面说说在Python中如何使用Redistribute的订阅服务。
  这里要举的例子是,Server端进行服务的订阅,而Client端进行消息的广播发送。
  1.方法一:Server端使用Redis操作+Client端使用Python交互器
  (1)Server端
  进入Redis操作界面:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis-2.8.9/src$ redis-cli  
127.0.0.1:6379>
  开始监听频道chan_107:
127.0.0.1:6379> SUBSCRIBE chan_107  
Reading messages... (press Ctrl-C to quit)
  
1) "subscribe"
  
2) "chan_107"
  
3) (integer) 1
  (2)Client端
  创建redis_connector.py文件,用以连接Server端Redis:
#!/usr/bin/env python  
import redis
  
r = redis.Redis(host='192.168.1.112',port=6379,db=0)
  在Python交互器中导入redis_connector,并发布消息:
>>> redis.r.publish('chan_107','hello my name is xpleaf')  
1L
  此时在Server端中就能看到Client端发布的消息了:
127.0.0.1:6379> SUBSCRIBE chan_107  
Reading messages... (press Ctrl-C to quit)
  
1) "subscribe"
  
2) "chan_107"
  
3) (integer) 1
  
1) "message"
  
2) "chan_107"
  
3) "hello my name is xpleaf"
  2.方法二:Server端使用Python程序+Client端使用Python交互器
  (1)Server端
  程序代码如下:
import redis_connector as redis  

  
channel = 'chan_107'    #频道应该要和发布者的一样,否则将无法订阅到发布者发布的消息
  

  
msg_queue = redis.r.pubsub() #bind listen instance
  
msg_queue.subscribe(channel)
  

  
while True:
  data = msg_queue.parse_response() #waiting for the publisher
  print data
  可以看到这里也导入了redis_connector模块用来连接本地的Redis数据库,跟Client端的类似,只是IP地址改为'localhost',如下:
#!/usr/bin/env python  
import redis
  
r = redis.Redis(host='localhost',port=6379,db=0)
  msg_queue.parse_response()即是用来响应Client端发布的消息,执行该程序,开始监听消息:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  
===>光标停在此处,开始监听Client端发布的消息
  (2)Client端
  创建redis_connector.py文件,用以连接Server端Redis:
#!/usr/bin/env python  
import redis
  
r = redis.Redis(host='192.168.1.112',port=6379,db=0)
  在Python交互器中导入redis_connector,并发布消息:
>>> redis.r.publish('chan_107','hello my name is yonghaoye')  
2L
  
>>> redis.r.publish('chan_107','second msg')
  
2L
  上面两步其实和方法一是一样的。
  此时在Server端中也可以订阅到Client端发布的消息了:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  
['message', 'chan_107', 'hello my name is yonghaoye']
  
['message', 'chan_107', 'second msg']
  
===>光标停在此处,继续监听Client端发布的消息
  3.方法三:Server端使用Python程序+Client端使用Python程序
  直接给出Client端的程序代码:
import redis_connector as redis  

  
for i in range(10):
  
        redis.r.publish('chan_107','This is the NO.%s msg I send to you' % i)
  Server端开始监听:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  Client端发布消息:
[root@moban ~]# python publish.py  在Server端中很快就可以监听到Client端发布的消息:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  
['message', 'chan_107', 'This is the NO.0 msg I send to you']
  
['message', 'chan_107', 'This is the NO.1 msg I send to you']
  
['message', 'chan_107', 'This is the NO.2 msg I send to you']
  
['message', 'chan_107', 'This is the NO.3 msg I send to you']
  
['message', 'chan_107', 'This is the NO.4 msg I send to you']
  
['message', 'chan_107', 'This is the NO.5 msg I send to you']
  
['message', 'chan_107', 'This is the NO.6 msg I send to you']
  
['message', 'chan_107', 'This is the NO.7 msg I send to you']
  
['message', 'chan_107', 'This is the NO.8 msg I send to you']
  
['message', 'chan_107', 'This is the NO.9 msg I send to you']



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631289-1-1.html 上篇帖子: 【Python之旅】第七篇(二):Redis使用基础 下篇帖子: 【Python之旅】第七篇(三):使用Redis订阅服务
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表