Python RabbitMQ sender.py远程队列

时间:2014-11-10 17:08:21

标签: python rabbitmq pika

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

连接到localhost的程序运行正常。但是连接到远程队列的程序不起作用。创建连接时必须出错,因为我看不到日志消息'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()

更新 这是我在尝试连接时遇到的错误。

ERROR:pika.adapters.base_connection:Socket Error on fd 3: 54 Traceback (most recent call last): File "remote-sender.py", line 10, in connection = pika.BlockingConnection(parameters) File "/Library/Python/2.7/site-packages/pika/adapters/base_connection.py", line 61, in init super(BaseConnection, self).init(parameters, on_open_callback) File "/Library/Python/2.7/site-packages/pika/connection.py", line 513, in init self._connect() File "/Library/Python/2.7/site-packages/pika/connection.py", line 804, in _connect self._adapter_connect() File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 146, in _adapter_connect self.process_data_events() File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 88, in process_data_events if self._handle_read(): File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 184, in _handle_read super(BlockingConnection, self)._handle_read() File "/Library/Python/2.7/site-packages/pika/adapters/base_connection.py", line 300, in _handle_read return self._handle_error(error) File "/Library/Python/2.7/site-packages/pika/adapters/base_connection.py", line 264, in _handle_error self._handle_disconnect() File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 181, in _handle_disconnect self._on_connection_closed(None, True) File "/Library/Python/2.7/site-packages/pika/adapters/blocking_connection.py", line 235, in _on_connection_closed raise exceptions.AMQPConnectionError(*self.closing) pika.exceptions.AMQPConnectionError: (0, '')

2 个答案:

答案 0 :(得分:3)

需要正确指定虚拟主机。在我的情况下,我的队列属于虚拟主机。但我试图连接到' /'。指定虚拟主机(下面的产品)后,我就能成功连接。

发现此link很有帮助。

#!/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()

答案 1 :(得分:2)

使用5672端口而不是15672! 它应该工作!