Search code examples
pythonrabbitmqamqppika

Python Pika and RabbitMQ Connecting to Publish


Trying to send data into a RabbitMQ queue using Python.

I haven't configured the server but it is running for other processes. I have a working login and can access the web output without problem.

The example code RabbitMQ gives for python uses Pika:

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='xxx.xxx.xxx.xxx:xxxxx'))
channel = connection.channel()

channel.queue_declare(queue='Test')

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

This runs and kicks me off with:

pika.exceptions.ConnectionClosed

Not a lot to go on but safe assumption is a login issue because the example code doesn't have any login info.

So I added it.

import pika
import sys
try:

    credentials = pika.PlainCredentials('username', 'password')


    connection = pika.BlockingConnection(pika.ConnectionParameters('xxx.xxx.xxx.xxx',
                                           xxxxx,
                                           'virtualhostnamehere',
                                           credentials,))

    channel = connection.channel()
    channel.queue_declare(queue='Test')

    channel.basic_publish(exchange='amq.direct',
                          body='Hello World!')
    print(" [x] Sent 'Hello World!'")
except:
    e = sys.exc_info()[0]
    print e

It seems to hang around for a good few minutes before giving me:

<class 'pika.exceptions.IncompatibleProtocolError'>

The server is running other services fine but I can't seem to pinpoint what I've done wrong.

The login is correct. The vhost name is correct. The host is correct. the exchange name is correct.

Would appreciate a point in the right direction.

Update:

I've tried using URLParameters as well with the same results.

parameters = pika.URLParameters('amqp://username:password@xxx.xxx.xxx.xxx:xxxxx/notmyvhostname')
connection = pika.BlockingConnection(parameters)

Solution

  • But I guess the port doesn't change anything. It's port 15672 and the login is the same as I used to get on the browser output.

    Use port 5672 - or whichever default port you have setup for AMQP listener. Port 15672 is for web UI access, which is done over HTTP, hence the incompatible protocol error