Create a gist now

Instantly share code, notes, and snippets.

AMQP consumer in python, for testing
#! /usr/bin/python
import sys
import pika
# Import all adapters for easier experimentation
from pika.adapters import *
pika.log.setup(pika.log.INFO, color=True)
connection = None
channel = None
queue = "test"
routing_key = ""
def on_connected(connection):
global channel
pika.log.info("demo_receive: Connected to RabbitMQ")
connection.channel(on_channel_open)
def on_channel_open(channel_):
global channel
channel = channel_
channel.basic_qos(prefetch_count=8)
pika.log.info("demo_receive: Received our Channel")
channel.queue_declare(queue=queue, durable=True,
exclusive=False, auto_delete=False,
callback=on_queue_declared)
def on_queue_declared(frame):
pika.log.info("demo_receive: Queue Declared")
channel.queue_bind(queue=queue, exchange='syslog', routing_key=routing_key)
channel.basic_consume(handle_delivery, queue=queue)
def handle_delivery(channel, method_frame, header_frame, body):
print "LOG",header_frame.headers
pika.log.info("Basic.Deliver %s delivery-tag %i: %s",
header_frame.content_type,
method_frame.delivery_tag,
body)
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
if __name__ == '__main__':
host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1'
parameters = pika.ConnectionParameters(host,5672)
connection = SelectConnection(parameters, on_connected)
try:
connection.ioloop.start()
except KeyboardInterrupt:
connection.close()
connection.ioloop.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment