Permalink
Browse files

Add examples for headers exchange.

  • Loading branch information...
1 parent c78609c commit 6a2ede4df64c1a849b797f6cfa75cb8fa8190c5e @majek committed Jul 21, 2011
Showing with 57 additions and 0 deletions.
  1. +24 −0 examples/emit_log_headers.py
  2. +33 −0 examples/receive_logs_headers.py
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+import sys
+sys.path.append("..")
+import puka
+
+client = puka.Client("amqp://localhost/")
+promise = client.connect()
+client.wait(promise)
+
+
+promise = client.exchange_declare(exchange='headers_logs', type='headers')
+client.wait(promise)
+
+argv = sys.argv[1:-1] if len(sys.argv) > 2 else ['anonymous:info']
+headers = dict(arg.split(':', 2) for arg in argv)
+
+message = sys.argv[-1] if len(sys.argv) > 1 else 'Hello World!'
+promise = client.basic_publish(exchange='headers_logs', routing_key='',
+ body=message,
+ headers=headers)
+client.wait(promise)
+
+print " [x] Sent %r %r" % (headers, message)
+client.close()
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+import sys
+sys.path.append("..")
+import puka
+
+client = puka.Client("amqp://localhost/")
+promise = client.connect()
+client.wait(promise)
+
+
+promise = client.exchange_declare(exchange='headers_logs', type='headers')
+client.wait(promise)
+
+promise = client.queue_declare(exclusive=True)
+queue_name = client.wait(promise)['queue']
+
+argv = sys.argv[1:]
+if not argv:
+ print >> sys.stderr, "Usage: %s [header:value]..." % (sys.argv[0],)
+ sys.exit(1)
+
+headers = dict(arg.split(':', 2) for arg in argv)
+headers['x-match'] = 'any'
+promise = client.queue_bind(exchange='headers_logs', queue=queue_name,
+ routing_key='', arguments=headers)
+client.wait(promise)
+
+print ' [*] Waiting for logs %r. To exit press CTRL+C' % (headers,)
+
+consume_promise = client.basic_consume(queue=queue_name, no_ack=True)
+while True:
+ msg_result = client.wait(consume_promise)
+ print " [x] %r:%r" % (msg_result['headers'], msg_result['body'])

0 comments on commit 6a2ede4

Please sign in to comment.