Search code examples
pythonpython-3.xrabbitmqmqttamqp

RabbitMQ bind exchange in Python


I need to bind an exchange (amq.topic) with a specific queue in Python. How I can do this?

Now I bind with the RabbitMQ's GUI.

I'm refering to this TAB of Rabbit

I've done only the binding of a queue with this code:

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


channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

result = channel.queue_declare(queue='coda-di-prova', exclusive=False)
queue_name = result.method.queue

channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key="test.*")

print(' [*] Waiting for logs. To exit press CTRL+C')


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


channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)

channel.start_consuming()

What I need to add in my code in order to get this result? Result IMAGE


Solution

  • I maybe didn't understand the question. But it is enough to use amq.topic

    import pika
    
    
    connection = pika.BlockingConnection(
    pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()
    
    
    result = channel.queue_declare(queue='coda-di-prova', exclusive=False)
    queue_name = result.method.queue
    
    channel.queue_bind(exchange='amq.topic', queue=queue_name, routing_key="test.*")
    
    print(' [*] Waiting for logs. To exit press CTRL+C')
    
    
    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body))
    
    
    channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True)
    
    channel.start_consuming()