Kreender
Kreender

Reputation: 294

Rabbitmq hello world connection only works on localhost (python)

I have this simple code taken from the rabbitmq tutorial (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()

It works but if I change localhost with the ip of my computer from my own computer or a computer in the same network:

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

I get this error:


>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, '')

I have no idea why, should I change something in the connection?

Upvotes: 4

Views: 9306

Answers (3)

Forward
Forward

Reputation: 31

for macOS, you need to edit /usr/local/etc/rabbitmq/rabbitmq-env.conf, where NODE_IP_ADDRESS=0.0.0.0 instead of localhost

Upvotes: 3

eandersson
eandersson

Reputation: 26352

As a follow up on @Gas response.

By default pika will connect using the default RabbitMQ credentials guest/guest. If you want to use your own credentials you need to provide your own PlainCredentials object.

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

On the server you would need to add a user with the appropriate permissions. You can do this using the web interface, or by command line. More details available in the link provided by @Gas.

rabbitmqctl add_user my_user password
rabbitmqctl set_permissions -p / my_user ".*" ".*" ".*"

These two command would give the user my_user all the permission it needs on virtual host /.

Upvotes: 8

Gabriele Santomaggio
Gabriele Santomaggio

Reputation: 22732

It's a user grant problem .

You are using the default user "guest ".

Please read this: Can't access RabbitMQ web management interface after fresh install

Upvotes: 4

Related Questions