Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Topic routing examples.

  • Loading branch information...
commit 4eb4b9a2ec3ee115b8cde16ffec05e24f76723aa 1 parent 2ff33f9
@majek majek authored
Showing with 54 additions and 0 deletions.
  1. +19 −0 python/emit_log_topic.py
  2. +35 −0 python/receive_logs_topic.py
View
19 python/emit_log_topic.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+import pika
+import sys
+
+connection = pika.AsyncoreConnection(pika.ConnectionParameters(
+ host='127.0.0.1',
+ credentials=pika.PlainCredentials('guest', 'guest')))
+channel = connection.channel()
+
+channel.exchange_declare(exchange='topic_logs',
+ type='topic')
+
+routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
+message = ' '.join(sys.argv[2:]) or 'Hello World!'
+channel.basic_publish(exchange='topic_logs',
+ routing_key=routing_key,
+ body=message)
+print " [x] Sent %r:%r" % (routing_key, message)
+connection.close()
View
35 python/receive_logs_topic.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+import pika
+import sys
+
+connection = pika.AsyncoreConnection(pika.ConnectionParameters(
+ host='127.0.0.1',
+ credentials=pika.PlainCredentials('guest', 'guest')))
+channel = connection.channel()
+
+channel.exchange_declare(exchange='topic_logs',
+ type='topic')
+
+result = channel.queue_declare(auto_delete=True)
+queue_name = result.queue
+
+binding_keys = sys.argv[1:]
+if not binding_keys:
+ print >> sys.stderr, "Usage: %s [binding_key]..." % (sys.argv[0],)
+ sys.exit(1)
+
+for binding_key in binding_keys:
+ channel.queue_bind(exchange='topic_logs',
+ queue=queue_name,
+ routing_key=binding_key)
+
+print ' [*] Waiting for logs. To exit press CTRL+C'
+
+def callback(ch, method, header, body):
+ print " [x] %r:%r" % (method.routing_key, body,)
+
+channel.basic_consume(callback,
+ queue=queue_name,
+ no_ack=True)
+
+pika.asyncore_loop()
Please sign in to comment.
Something went wrong with that request. Please try again.