python第五十七天-- 补上笔记

RabbitMQ队列:

发送端:

 1 #!usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 # Author calmyan 
 4 #python 
 5 #2017/6/26    16:08
 6 #__author__='Administrator'
 7 import pika
 8 connetion =pika.BlockingConnection(
 9     pika.ConnectionParameters('localhost')#创建连接
10 )
11 chann_1=connetion.channel()#生成一个管道
12 
13 chann_1.queue_declare(queue='hello')#生成对列
14 
15 chann_1.basic_publish(exchange='',#
16                       routing_key='hello',#使用的对列
17                       body='发送的内容....'
18                       )
19 print('[xxx]:发送了内容....')
20 connetion.close()#关闭连接
View Code

接收端:

 1 #!usr/bin/env python
 2 #-*-coding:utf-8-*-
 3 # Author calmyan 
 4 #python 
 5 #2017/6/26    18:28
 6 #__author__='Administrator'
 7 import pika
 8 connetion =pika.BlockingConnection(
 9     pika.ConnectionParameters('localhost')#创建连接
10 )
11 chann_1=connetion.channel()#生成一个管道
12 
13 chann_1.queue_declare(queue='hello')#生成对列
14 
15 def callback(ch,method,properties,body):
16     print(ch,method,properties)#ch 管道内存对象,  method ,队列等 信息
17     print('[xxx] 回调函数的内容 %r'%body.decode())
18 
19 chann_1.basic_consume(#收消息
20         callback,#如果收到消息就调用  函数
21         queue='hello',#收消息的对列
22         no_ack=True#
23                       )
24 print('运行一直收消息, Ctrl+C 退出!')
25 chann_1.start_consuming()#开始接收消息
View Code

 

posted @ 2017-07-20 20:02  莫柔落切  阅读(231)  评论(0编辑  收藏  举报