有没有大佬帮我解决下问题??谢谢!在线等,捉急,要哭了!

博主在使用Python通过PIKA库操作RabbitMQ时产生错误,给出了Python 3.7、pika 1.0.1、RabbitMQ 3.7.14的版本信息,还提及了具体代码send.py和receive.py。

这是我在使用Python通过PIKA库操作RabbitMQ时产生的错误
这是我在使用Python通过PIKA库操作RabbitMQ时产生的错误
python版本是3.7 pika库是1.0.1 RabbitMQ版本是rabbitmq_server-3.7.14
下面是具体代码:send.py

import pika
username="guest"
pwd="guest"
user_pwd=pika.PlainCredentials(username,pwd)
#建立实例
connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost',credentials=user_pwd,heartbeat=0))#默认端口5672 可以不写
#声明一个管道可以不写 
channel = connection.channel()
#声明queue  队列
channel.queue_declare(queue='hello')
# 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='hello', # queue名字
                      body='Hello World! MY name is LWQ')# 消息内容
print (" [x] Sent 'Hello World!'")
connection.close() # 队列关闭

receive.py:

import pika
username="guest"
pwd="guest"
user_pwd=pika.PlainCredentials(username,pwd)
print (' [*] Waiting for messages. To exit press CTRL+C')   
connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost',credentials=user_pwd,heartbeat=0))#默认端口5672 可以不写
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
    print (" [x] Received %r" % body )
    ch.basic_ack(delivery_tag = method.delivery_tag)  # 告诉生成者,消息处理完成
channel.basic_qos(prefetch_count=1)  # 类似权重,按能力分发,如果有一个消息,就不在给你发
channel.basic_consume('hello',callback,consumer_tag="hello",auto_ack=True,exclusive=True)
channel.start_consuming()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值