From 7155d76a114f52144f96aa0273e8841f4d8be8ad Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 14 Nov 2012 07:52:32 -0500 Subject: [PATCH] Filling in consumer side --- .../chapter02/hello_world_consumer.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 sylvilagus/sylvilagus/chapter02/hello_world_consumer.py diff --git a/sylvilagus/sylvilagus/chapter02/hello_world_consumer.py b/sylvilagus/sylvilagus/chapter02/hello_world_consumer.py new file mode 100644 index 0000000..395752c --- /dev/null +++ b/sylvilagus/sylvilagus/chapter02/hello_world_consumer.py @@ -0,0 +1,46 @@ +from __future__ import print_function + +import sys + +import pika + + +def msg_consumer(channel, method, header, body): + channel.basic_ack(delivery_tag=method.delivery_tag) + if body == 'quit': + channel.basic_cancel(consumer_tag='hello-consumer') + channel.stop_consuming() + else: + print(body) + return + + +def main(): + credentials = pika.PlainCredentials('guest', 'guest') + conn_params = pika.ConnectionParameters('localhost', + credentials=credentials) + conn_broker = pika.BlockingConnection(conn_params) + channel = conn_broker.channel() + channel.exchange_declare(exchange='hello-exchange', + type='direct', + passive=False, + durable=True, + auto_delete=False) + + channel.queue_declare(queue='hello-queue') + channel.queue_bind(queue='hello-queue', + exchange='hello-exchange', + routing_key='hola') + + channel.basic_consume(msg_consumer, + queue='hello-queue', + consumer_tag='hello-consumer') + + print('consuming...') + channel.start_consuming() + + return 0 + + +if __name__ == '__main__': + sys.exit(main())