Python RabbitMQ sender.py远程队列

Python RabbitMQ sender.py远程队列,python,rabbitmq,pika,Python,Rabbitmq,Pika,我正在尝试向远程计算机中的rabbit mq队列发送消息。我正在学习教程 连接到本地主机的程序工作正常。但是连接到远程队列的程序不起作用。创建连接时一定会出错,因为我看不到日志消息“connection created” 我已验证我可以从我的计算机访问remotehost和端口,并且凭据正确。我可以访问 http://remote-host:15672/#/queues 我错过了什么明显的东西吗 本地 #!/usr/bin/env python import pika connection

我正在尝试向远程计算机中的rabbit mq队列发送消息。我正在学习教程

连接到本地主机的程序工作正常。但是连接到远程队列的程序不起作用。创建连接时一定会出错,因为我看不到日志消息“connection created”

我已验证我可以从我的计算机访问remotehost和端口,并且凭据正确。我可以访问

http://remote-host:15672/#/queues
我错过了什么明显的东西吗

本地

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')

channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
远程

#!/usr/bin/env python
import pika

# this queue is the destination queue
credentials = pika.PlainCredentials('xxxx', 'xxxx')
parameters = pika.ConnectionParameters('remote-host', 15672, '/', credentials)
connection = pika.BlockingConnection(parameters)
print " connection created"

channel = connection.channel()
channel.queue_declare(queue='hello')

channel.basic_publish(exchange='helloEx', routing_key='', body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
更新 这是我在尝试连接时遇到的错误


错误:pika.adapters.base_连接:fd 3:54回溯上的套接字错误(最近一次调用last):
文件“remote sender.py”,第10行,in connection=pika.BlockingConnection(参数)
文件“/Library/Python/2.7/site packages/pika/adapters/base_connection.py”,第61行,在init
超级(BaseConnection,self)。init(参数,在\u open\u回调上)
文件“/Library/Python/2.7/site packages/pika/connection.py”,第513行,在initself.\u connect()中
文件“/Library/Python/2.7/site packages/pika/connection.py”,第804行,在适配器连接()中
文件“/Library/Python/2.7/site packages/pika/adapters/blocking_connection.py”,第146行,in_adapter_connect
self.process\u data\u events()
文件“/Library/Python/2.7/site packages/pika/adapters/blocking_connection.py”,第88行,进程内数据事件
如果self.\u handle\u read():
文件“/Library/Python/2.7/site packages/pika/adapters/blocking_connection.py”,第184行,在_handle_read中
超级(阻塞连接,自)。\u句柄\u读取()
文件“/Library/Python/2.7/site-packages/pika/adapters/base\u-connection.py”,第300行,在“句柄”中读取
返回self.\u handle\u错误(error)
文件“/Library/Python/2.7/site packages/pika/adapters/base_connection.py”,第264行,in_handle_error
自身。_手柄_断开()
文件“/Library/Python/2.7/site packages/pika/adapters/blocking_connection.py”,第181行,in_handle_disconnect
self.\u打开\u连接\u关闭(无,为真)
文件“/Library/Python/2.7/site packages/pika/adapters/blocking_connection.py”,第235行,in_on_connection_closed
引发异常。AMQPConnectionError(*自动关闭)
pika.exceptions.amqConnectionError:(0,,)

使用5672端口,而不是15672!
它应该有用

需要正确指定虚拟主机。在我的例子中,我的队列属于虚拟主机。但我正试图连接到“/”。在指定虚拟主机(下面的产品)后,我能够成功连接

我发现这很有帮助

#!/usr/bin/env python
import pika

# this queue is the destination queue
credentials = pika.PlainCredentials('xxxx', 'xxxx')
parameters = pika.ConnectionParameters('remote-host', 5672, 'product', credentials)
connection = pika.BlockingConnection(parameters)
print " connection created"

channel = connection.channel()
channel.queue_declare(queue='hello')

channel.basic_publish(exchange='helloEx', routing_key='', body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()

您是否正在连接来宾帐户?来宾帐户只能通过本地主机连接@kponz不,不是用客户账户,是我做的。但我得到了同样的结果。我验证了我可以访问该端口。用我得到的错误更新了问题。知道有什么问题吗?谢谢@ravindrab,抱歉我修改了端口正确的是5672!我尝试使用正确的端口5672。用我看到的错误更新了答案。如果这不是防火墙问题,您应该检查rabbitmq日志,查看连接是否到达rabbitmq。我建议读这篇文章:问题是我没有指定vhost
parameters=pika.ConnectionParameters('remote-host',5672,'product',credentials)
成功了。我正试图在本地主机上使用RabbitMQ,但使用起来很困难,你能给我一些关于这方面的建议吗?你的具体问题是什么?