浏览此处的 Rabbit MQ Pika HelloWorld 教程: https://www.rabbitmq.com/tutorials/tutorial-one-python.html
问题是,每当我运行接收脚本时,我都会收到此错误:
Traceback (most recent call last):
File "receive.py", line 5, in <module>
pika.ConnectionParameters(host='localhost'))
File "C:\Users\Colin Warn\PycharmProjects\untitled2\venv\lib\site-packages\pika\adapters\blocking_connection.py", line 360, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "C:\Users\Colin Warn\PycharmProjects\untitled2\venv\lib\site-packages\pika\adapters\blocking_connection.py", line 451, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError
这是我尝试运行的代码:
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(
queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
非常感谢任何帮助。提前非常感谢您。
您所需要做的就是在您的 PC 上安装 RabbitMQ。您可以简单地在另一个终端中使用以下命令运行 docker,然后重新运行您的代码
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management
您需要在您的机器上安装 RabbitMQ: https://rabbitmq.com/download.html
安装 RabbitMQ 服务器后重新运行脚本。
请按照以下步骤操作:
我得到了一个不同的解决方案,而不是使用 host='localhost',而是使用rabbitmq docker container ipaddress(在我的例子中是172.17.0.2),你可以使用以下命令获取该ip:
docker inspect <container_id>
对我来说,这个问题是在我更新系统时出现的。
可能的原因是rabbitmq-server未安装或rabbitmq-server未运行。
尝试在 ubuntu 中运行以下命令
sudo systemctl status rabbimtmq-server
如果服务器未运行,请使用以下命令启动服务器。
sudo systemctl restart rabbitmq-server
如果一切正常,那么您的代码将毫无问题地运行。
如果您无法启动/重新启动服务器,请从下面的链接重新安装rabbitmq。
就我而言,我通过重新安装rabbitmq解决了问题。
我也面临同样的问题。然后我在我的托管设备中启用(打开)5672 端口。然后就解决了。