Skip to content
Browse files

Puka: four and five

  • Loading branch information...
1 parent be12be3 commit 75863fb478cd592b690f1ded51b29ed0f0c09d43 @majek majek committed Jul 6, 2011
View
19 python-puka/README.md
@@ -9,18 +9,18 @@ to [Puka](https://github.com/majek/puka) Python library.
Now you can install `puka` using Pip:
- $ pip install puka
+ pip install puka
You may need to install `pip` first:
* On Ubuntu:
- $ sudo apt-get install python-pip git-core
+ sudo apt-get install python-pip git-core
- * On Debian:
+ * On Debian:
- $ sudo apt-get install python-setuptools
- $ sudo easy_install pip
+ sudo apt-get install python-setuptools
+ sudo easy_install pip
## Code
@@ -35,10 +35,17 @@ You may need to install `pip` first:
python new_task.py
python worker.py
-[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html)
+[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html):
python receive_logs.py
python emit_log.py
+[Tutorial four: Routing](http://www.rabbitmq.com/tutorial-four-python.html):
+ python receive_logs_direct.py
+ python emit_log_direct.py
+[Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html):
+
+ python receive_logs_topic.py
+ python emit_log_topic.py
View
20 python-puka/emit_log_direct.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+import puka
+import sys
+
+client = puka.Client("amqp://localhost/")
+promise = client.connect()
+client.wait(promise)
+
+
+promise = client.exchange_declare(exchange='direct_logs', type='direct')
+client.wait(promise)
+
+severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
+message = ' '.join(sys.argv[2:]) or 'Hello World!'
+promise = client.basic_publish(exchange='direct_logs', routing_key=severity,
+ body=message)
+client.wait(promise)
+
+print " [x] Sent %r:%r" % (severity, message)
+client.close()
View
20 python-puka/emit_log_topic.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+import puka
+import sys
+
+client = puka.Client("amqp://localhost/")
+promise = client.connect()
+client.wait(promise)
+
+
+promise = client.exchange_declare(exchange='topic_logs', type='topic')
+client.wait(promise)
+
+routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
+message = ' '.join(sys.argv[2:]) or 'Hello World!'
+promise = client.basic_publish(exchange='topic_logs', routing_key=routing_key,
+ body=message)
+client.wait(promise)
+
+print " [x] Sent %r:%r" % (routing_key, message)
+client.close()
View
32 python-puka/receive_logs_direct.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+import puka
+import sys
+
+client = puka.Client("amqp://localhost/")
+promise = client.connect()
+client.wait(promise)
+
+
+promise = client.exchange_declare(exchange='direct_logs', type='direct')
+client.wait(promise)
+
+promise = client.queue_declare(exclusive=True)
+queue_name = client.wait(promise)['queue']
+
+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:
+ promise = client.queue_bind(exchange='direct_logs', queue=queue_name,
+ routing_key=severity)
+ client.wait(promise)
+
+
+print ' [*] Waiting for logs. To exit press CTRL+C'
+
+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['routing_key'], msg_result['body'])
View
32 python-puka/receive_logs_topic.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+import puka
+import sys
+
+client = puka.Client("amqp://localhost/")
+promise = client.connect()
+client.wait(promise)
+
+
+promise = client.exchange_declare(exchange='topic_logs', type='topic')
+client.wait(promise)
+
+promise = client.queue_declare(exclusive=True)
+queue_name = client.wait(promise)['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:
+ promise = client.queue_bind(exchange='topic_logs', queue=queue_name,
+ routing_key=binding_key)
+ client.wait(promise)
+
+
+print ' [*] Waiting for logs. To exit press CTRL+C'
+
+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['routing_key'], msg_result['body'])

0 comments on commit 75863fb

Please sign in to comment.
Something went wrong with that request. Please try again.