Python RabbitMQ sender.py remote queue
I am trying to send a message to a mq rabbit queue on a remote machine. I am following this tutorial.
The program to connect to localhost works fine. But the program for connecting to the remote queue doesn't work. The error must be related to creating a connection because I don't see the "connection created" log message.
I have verified that I can access the remote host, port from my machine and the credentials are correct. I can access
http://remote-host:15672/#/queues
Am I not seeing anything 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 get when I try 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, '')
source to share
The virtual host must be correct. In my case, my queues were owned by a virtual host. But I tried to connect to '/'. After specifying the virtual host ( product ), I was able to connect successfully.
Found the link 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()
source to share