Skip to content

Commit 75863fb

Browse files
committed
Puka: four and five
1 parent be12be3 commit 75863fb

File tree

5 files changed

+117
-6
lines changed

5 files changed

+117
-6
lines changed

python-puka/README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ to [Puka](https://github.com/majek/puka) Python library.
99

1010
Now you can install `puka` using Pip:
1111

12-
$ pip install puka
12+
pip install puka
1313

1414
You may need to install `pip` first:
1515

1616
* On Ubuntu:
1717

18-
$ sudo apt-get install python-pip git-core
18+
sudo apt-get install python-pip git-core
1919

20-
* On Debian:
20+
* On Debian:
2121

22-
$ sudo apt-get install python-setuptools
23-
$ sudo easy_install pip
22+
sudo apt-get install python-setuptools
23+
sudo easy_install pip
2424

2525

2626
## Code
@@ -35,10 +35,17 @@ You may need to install `pip` first:
3535
python new_task.py
3636
python worker.py
3737

38-
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html)
38+
[Tutorial three: Publish/Subscribe](http://www.rabbitmq.com/tutorial-three-python.html):
3939

4040
python receive_logs.py
4141
python emit_log.py
4242

43+
[Tutorial four: Routing](http://www.rabbitmq.com/tutorial-four-python.html):
4344

45+
python receive_logs_direct.py
46+
python emit_log_direct.py
4447

48+
[Tutorial five: Topics](http://www.rabbitmq.com/tutorial-five-python.html):
49+
50+
python receive_logs_topic.py
51+
python emit_log_topic.py

python-puka/emit_log_direct.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
import puka
3+
import sys
4+
5+
client = puka.Client("amqp://localhost/")
6+
promise = client.connect()
7+
client.wait(promise)
8+
9+
10+
promise = client.exchange_declare(exchange='direct_logs', type='direct')
11+
client.wait(promise)
12+
13+
severity = sys.argv[1] if len(sys.argv) > 1 else 'info'
14+
message = ' '.join(sys.argv[2:]) or 'Hello World!'
15+
promise = client.basic_publish(exchange='direct_logs', routing_key=severity,
16+
body=message)
17+
client.wait(promise)
18+
19+
print " [x] Sent %r:%r" % (severity, message)
20+
client.close()

python-puka/emit_log_topic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
import puka
3+
import sys
4+
5+
client = puka.Client("amqp://localhost/")
6+
promise = client.connect()
7+
client.wait(promise)
8+
9+
10+
promise = client.exchange_declare(exchange='topic_logs', type='topic')
11+
client.wait(promise)
12+
13+
routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
14+
message = ' '.join(sys.argv[2:]) or 'Hello World!'
15+
promise = client.basic_publish(exchange='topic_logs', routing_key=routing_key,
16+
body=message)
17+
client.wait(promise)
18+
19+
print " [x] Sent %r:%r" % (routing_key, message)
20+
client.close()

python-puka/receive_logs_direct.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
import puka
3+
import sys
4+
5+
client = puka.Client("amqp://localhost/")
6+
promise = client.connect()
7+
client.wait(promise)
8+
9+
10+
promise = client.exchange_declare(exchange='direct_logs', type='direct')
11+
client.wait(promise)
12+
13+
promise = client.queue_declare(exclusive=True)
14+
queue_name = client.wait(promise)['queue']
15+
16+
severities = sys.argv[1:]
17+
if not severities:
18+
print >> sys.stderr, "Usage: %s [info] [warning] [error]" % (sys.argv[0],)
19+
sys.exit(1)
20+
21+
for severity in severities:
22+
promise = client.queue_bind(exchange='direct_logs', queue=queue_name,
23+
routing_key=severity)
24+
client.wait(promise)
25+
26+
27+
print ' [*] Waiting for logs. To exit press CTRL+C'
28+
29+
consume_promise = client.basic_consume(queue=queue_name, no_ack=True)
30+
while True:
31+
msg_result = client.wait(consume_promise)
32+
print " [x] %r:%r" % (msg_result['routing_key'], msg_result['body'])

python-puka/receive_logs_topic.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python
2+
import puka
3+
import sys
4+
5+
client = puka.Client("amqp://localhost/")
6+
promise = client.connect()
7+
client.wait(promise)
8+
9+
10+
promise = client.exchange_declare(exchange='topic_logs', type='topic')
11+
client.wait(promise)
12+
13+
promise = client.queue_declare(exclusive=True)
14+
queue_name = client.wait(promise)['queue']
15+
16+
binding_keys = sys.argv[1:]
17+
if not binding_keys:
18+
print >> sys.stderr, "Usage: %s [binding_key]..." % (sys.argv[0],)
19+
sys.exit(1)
20+
21+
for binding_key in binding_keys:
22+
promise = client.queue_bind(exchange='topic_logs', queue=queue_name,
23+
routing_key=binding_key)
24+
client.wait(promise)
25+
26+
27+
print ' [*] Waiting for logs. To exit press CTRL+C'
28+
29+
consume_promise = client.basic_consume(queue=queue_name, no_ack=True)
30+
while True:
31+
msg_result = client.wait(consume_promise)
32+
print " [x] %r:%r" % (msg_result['routing_key'], msg_result['body'])

0 commit comments

Comments
 (0)