资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

rabbitmq远程消费者生产者发送端接收端实例

rabbit_remote_send_procedure.py

成都创新互联专注于无极网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供无极营销型网站建设,无极网站制作、无极网页设计、无极网站官网定制、重庆小程序开发公司服务,打造无极网络公司原创品牌,更为您提供无极网站排名全网营销落地服务。

#!_*_coding:utf-8_*_
importpika
credentials=pika.PlainCredentials('lwb','123456')

connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.81.100',port=5672,virtual_host='/',credentials=credentials))#rabbit默认端口5672 建立一个基本的 socket连接
channel = connection.channel()#声明一个管道 在管道里面发消息

# 声明queue
channel.queue_declare(queue='hello5')

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                      routing_key='hello5',#queue名字
                      body='Hello World!')#body 发送的消息
print(" [x] Sent 'Hello World!'")
connection.close()

 

 

rabbitmq_recive_consumer.py

#!_*_coding:utf-8_*_
__author__ ='Alex Li'
import
pika
credentials=pika.PlainCredentials('lwb','123456')
connection = pika.BlockingConnection(pika.ConnectionParameters(host='192.168.81.100',port=5672,virtual_host='/',credentials=credentials))#rabbit默认端口5672 建立一个基本的 socket连接
channel = connection.channel()#声明一个管道 在管道里面收消息

# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
channel.queue_declare(queue='hello1')#声明queue


defcallback(ch, method, properties, body):
    print("---->",ch,method,properties)#ch 管道内存对象地址 method:发给queue的信息
    print(" [x] Received %r"% body)


channel.basic_consume(#消费消息
                      callback,#如果收到消息,就调用CALLBACK函数来处理消息
                      queue='hello1',#从哪个队列里收消息
                      no_ack=True
                     
)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()#启动 开始收消息 一直收,没有就卡主


本文标题:rabbitmq远程消费者生产者发送端接收端实例
新闻来源:http://cdkjz.cn/article/ggeogs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220