Python RabbitMQ sender.py remote queue

4k views Asked by At

I am trying to send a message to a rabbit mq queue in a remote machine. I am following this tutorial.

The program to connect to localhost works fine. But the program to connect to remote queue doesn't work. The error must lie when creating the connection, because I can't see the log message 'connection created'.

I have verified I can access remotehost, port from my machine and the credentials are correct. I can access

http://remote-host:15672/#/queues

Am I missing any thing obvious?

Local

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

Remote

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

Update This is the error I am getting while trying to connect.

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

There are 2 answers

2
ravindrab On BEST ANSWER

The virtual host needs to be specified correctly. In my case my queues belonged to a virtual host. But I was trying to connect to '/'. After specifying the virtual host (product below) I was able to connect successfully.

Found this link is helpful.

#!/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()
5
Gabriele Santomaggio On

Use 5672 port and not 15672 ! It should work !