Rabbitmq hello world连接仅适用于localhost(python)

时间:2015-02-27 20:16:47

标签: python rabbitmq

我从rabbitmq教程(http://www.rabbitmq.com/tutorials/tutorial-one-python.html

中获取了这个简单的代码
import pika
import logging

logging.basicConfig()

connection = pika.BlockingConnection(pika.ConnectionParameters(
       host='localhost'))

channel = connection.channel()

channel.queue_declare(queue='hello')

print ' [*] Waiting for messages. To exit press CTRL+C'

def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

channel.start_consuming()

它可以工作,但是如果我用我自己的计算机或同一网络中的计算机上的计算机ip更改localhost:

connection = pika.BlockingConnection(pika.ConnectionParameters(
       host='192.168.60.126'))

我收到此错误:


>python rabbitMQReceiver.py
ERROR:pika.adapters.base_connection:Socket Error on fd 316: 10054
Traceback (most recent call last):
  File "rabbitMQReceiver.py", line 7, in <module>
    host='192.168.60.126'))
  File "C:\Python27\lib\site-packages\pika\adapters\base_connection.py", line 61, in __init__
    super(BaseConnection, self).__init__(parameters, on_open_callback)
  File "C:\Python27\lib\site-packages\pika\connection.py", line 513, in __init__
    self._connect()
  File "C:\Python27\lib\site-packages\pika\connection.py", line 804, in _connect
    self._adapter_connect()
  File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 146, in _adapter_connect
    self.process_data_events()
  File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 88, in process_data_events
    if self._handle_read():
  File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 184, in _handle_read
    super(BlockingConnection, self)._handle_read()
  File "C:\Python27\lib\site-packages\pika\adapters\base_connection.py", line 300, in _handle_read
    return self._handle_error(error)
  File "C:\Python27\lib\site-packages\pika\adapters\base_connection.py", line 264, in _handle_error
    self._handle_disconnect()
  File "C:\Python27\lib\site-packages\pika\adapters\blocking_connection.py", line 181, in _handle_disconnect
    self._on_connection_closed(None, True)
  File "C:\Python27\lib\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)

这是用户授权问题。

您使用的是默认用户“guest”。

请阅读: Can't access RabbitMQ web management interface after fresh install

答案 1 :(得分:1)

作为对@Gas回复的跟进。

默认情况下,pika将使用默认的RabbitMQ凭据guest/guest进行连接。如果您想使用自己的凭据,则需要提供自己的PlainCredentials对象。

credentials = pika.PlainCredentials(username='my_user', password='password')
connection = \
    pika.BlockingConnection(pika.ConnectionParameters(host='192.168.60.126',
                                                      credentials=credentials))

在服务器上,您需要添加具有适当权限的用户。您可以使用Web界面或命令行执行此操作。更多细节可在@Gas。

提供的链接中找到
rabbitmqctl add_user my_user password
rabbitmqctl set_permissions -p / my_user ".*" ".*" ".*"

这两个命令将为用户my_user提供虚拟主机/上所需的所有权限。