【发布时间】:2014-11-10 17:08:21
【问题描述】:
我正在尝试向远程机器中的 rabbit mq 队列发送消息。我正在关注this 教程。
连接到 localhost 的程序运行良好。但是连接到远程队列的程序不起作用。错误一定是在创建连接时出现的,因为我看不到“连接已创建”的日志消息。
我已经验证我可以从我的机器访问远程主机、端口并且凭据是正确的。我可以访问
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, '')
【问题讨论】:
-
您正在连接访客帐户吗?来宾帐户只能通过 localhost 连接。 rabbitmq.com/access-control.html
-
@kponz No. 不使用访客帐户