Chad Burrus avatar Chad Burrus committed a0363cf

tutorial 4

Comments (0)

Files changed (2)

tut4/emit_logs.py

+#!/usr/bin/env python
+import pika
+import sys
+
+# make connection to RabbitMQ server
+connection = pika.BlockingConnection(pika.ConnectionParameters(
+                 'localhost'))
+channel = connection.channel()
+
+# make an exchange that will pass log information to everyone
+channel.exchange_declare(exchange='direct_logs', type='direct')
+
+# Send the message
+severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
+message = ' '.join(sys.argv[2:]) or 'Hello World!'
+channel.basic_publish(exchange='direct_logs',
+                      routing_key=severity,
+                      body=message)
+
+# Print a handy message
+print " [x] Sent %s:%s " % (severity, message)
+
+# Close up shop, flushing buffers and the like
+connection.close()

tut4/receive_logs.py

+#!/usr/bin/env python
+import pika
+import sys
+
+# make connection to RabbitMQ server
+connection = pika.BlockingConnection(pika.ConnectionParameters(
+        host='localhost'))
+channel = connection.channel()
+
+# make an exchange that will pass log information to everyone
+channel.exchange_declare(exchange='direct_logs', type='direct')
+
+# init a random queue so we don't get any old messages, and make sure it's nuked when the consumer dies?  
+result = channel.queue_declare(exclusive=True)
+queue_name = result.method.queue
+
+# add in the desired severities
+severities = sys.argv[1:]
+if not severities:
+  print >>sys.stderr, "Usage: %s [info] [warning] [error]" % sys.argv[0]
+  sys.exit(1)
+
+for severity in severities:
+  # bind the new anonymous queue to the logs exchange so it can receive messages
+  channel.queue_bind(exchange='direct_logs', queue=queue_name, routing_key=severity)
+
+# Print a handy message
+print ' [*] Waiting for logs. To exit press CTRL+C'
+
+# The thing to do when a message is received
+def callback(ch, method, properties, body):
+  print " [x] %r:%r" % (method.routing_key, body,)
+
+# Set up the consumer
+channel.basic_consume(callback,
+                      queue=queue_name,
+                      no_ack=True)
+
+# Start consuming
+channel.start_consuming()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.