Skip to content

Commit e1553ba

Browse files
Apply rabbitmq#275, rabbitmq#276 to more Python tutorials
1 parent 0076462 commit e1553ba

File tree

4 files changed

+87
-56
lines changed

4 files changed

+87
-56
lines changed

Diff for: python/receive.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def main():
88
channel.queue_declare(queue='hello')
99

1010
def callback(ch, method, properties, body):
11-
print(" [x] Received %r" % body)
11+
print(" [x] Received %r" % body.decode())
1212

1313
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
1414

Diff for: python/receive_logs.py

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
#!/usr/bin/env python
2-
import pika
2+
import pika, sys, os
33

4-
connection = pika.BlockingConnection(
5-
pika.ConnectionParameters(host='localhost'))
6-
channel = connection.channel()
4+
def main():
5+
connection = pika.BlockingConnection(
6+
pika.ConnectionParameters(host='localhost'))
7+
channel = connection.channel()
78

8-
channel.exchange_declare(exchange='logs', exchange_type='fanout')
9+
channel.exchange_declare(exchange='logs', exchange_type='fanout')
910

10-
result = channel.queue_declare(queue='', exclusive=True)
11-
queue_name = result.method.queue
11+
result = channel.queue_declare(queue='', exclusive=True)
12+
queue_name = result.method.queue
1213

13-
channel.queue_bind(exchange='logs', queue=queue_name)
14+
channel.queue_bind(exchange='logs', queue=queue_name)
1415

15-
print(' [*] Waiting for logs. To exit press CTRL+C')
16+
def callback(ch, method, properties, body):
17+
print(" [x] %r" % body.decode())
1618

19+
print(' [*] Waiting for logs. To exit press CTRL+C')
20+
channel.basic_consume(
21+
queue=queue_name, on_message_callback=callback, auto_ack=True)
1722

18-
def callback(ch, method, properties, body):
19-
print(" [x] %r" % body)
23+
channel.start_consuming()
2024

2125

22-
channel.basic_consume(
23-
queue=queue_name, on_message_callback=callback, auto_ack=True)
24-
25-
channel.start_consuming()
26+
if __name__ == '__main__':
27+
try:
28+
main()
29+
except KeyboardInterrupt:
30+
print('Interrupted')
31+
try:
32+
sys.exit(0)
33+
except SystemExit:
34+
os._exit(0)

Diff for: python/receive_logs_direct.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
#!/usr/bin/env python
2-
import pika
3-
import sys
2+
import pika, sys, os
43

5-
connection = pika.BlockingConnection(
4+
def main():
5+
connection = pika.BlockingConnection(
66
pika.ConnectionParameters(host='localhost'))
7-
channel = connection.channel()
7+
channel = connection.channel()
88

9-
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
9+
channel.exchange_declare(exchange='direct_logs', exchange_type='direct')
1010

11-
result = channel.queue_declare(queue='', exclusive=True)
12-
queue_name = result.method.queue
11+
result = channel.queue_declare(queue='', exclusive=True)
12+
queue_name = result.method.queue
1313

14-
severities = sys.argv[1:]
15-
if not severities:
16-
sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
17-
sys.exit(1)
14+
severities = sys.argv[1:]
15+
if not severities:
16+
sys.stderr.write("Usage: %s [info] [warning] [error]\n" % sys.argv[0])
17+
sys.exit(1)
1818

19-
for severity in severities:
20-
channel.queue_bind(
21-
exchange='direct_logs', queue=queue_name, routing_key=severity)
19+
for severity in severities:
20+
channel.queue_bind(
21+
exchange='direct_logs', queue=queue_name, routing_key=severity)
2222

23-
print(' [*] Waiting for logs. To exit press CTRL+C')
23+
print(' [*] Waiting for logs. To exit press CTRL+C')
2424

2525

26-
def callback(ch, method, properties, body):
27-
print(" [x] %r:%r" % (method.routing_key, body))
26+
def callback(ch, method, properties, body):
27+
print(" [x] %r:%r" % (method.routing_key, body.decode()))
2828

2929

30-
channel.basic_consume(
31-
queue=queue_name, on_message_callback=callback, auto_ack=True)
30+
channel.basic_consume(
31+
queue=queue_name, on_message_callback=callback, auto_ack=True)
3232

33-
channel.start_consuming()
33+
channel.start_consuming()
34+
35+
36+
if __name__ == '__main__':
37+
try:
38+
main()
39+
except KeyboardInterrupt:
40+
print('Interrupted')
41+
try:
42+
sys.exit(0)
43+
except SystemExit:
44+
os._exit(0)

Diff for: python/receive_logs_topic.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
11
#!/usr/bin/env python
2-
import pika
3-
import sys
2+
import pika, sys, os
43

5-
connection = pika.BlockingConnection(
4+
def main():
5+
connection = pika.BlockingConnection(
66
pika.ConnectionParameters(host='localhost'))
7-
channel = connection.channel()
7+
channel = connection.channel()
88

9-
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
9+
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')
1010

11-
result = channel.queue_declare(queue='', exclusive=True)
12-
queue_name = result.method.queue
11+
result = channel.queue_declare(queue='', exclusive=True)
12+
queue_name = result.method.queue
1313

14-
binding_keys = sys.argv[1:]
15-
if not binding_keys:
16-
sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
17-
sys.exit(1)
14+
binding_keys = sys.argv[1:]
15+
if not binding_keys:
16+
sys.stderr.write("Usage: %s [binding_key]...\n" % sys.argv[0])
17+
sys.exit(1)
1818

19-
for binding_key in binding_keys:
20-
channel.queue_bind(
21-
exchange='topic_logs', queue=queue_name, routing_key=binding_key)
19+
for binding_key in binding_keys:
20+
channel.queue_bind(
21+
exchange='topic_logs', queue=queue_name, routing_key=binding_key)
2222

23-
print(' [*] Waiting for logs. To exit press CTRL+C')
23+
print(' [*] Waiting for logs. To exit press CTRL+C')
2424

2525

26-
def callback(ch, method, properties, body):
27-
print(" [x] %r:%r" % (method.routing_key, body))
26+
def callback(ch, method, properties, body):
27+
print(" [x] %r:%r" % (method.routing_key, body.decode()))
2828

2929

30-
channel.basic_consume(
31-
queue=queue_name, on_message_callback=callback, auto_ack=True)
30+
channel.basic_consume(
31+
queue=queue_name, on_message_callback=callback, auto_ack=True)
3232

33-
channel.start_consuming()
33+
channel.start_consuming()
34+
35+
36+
if __name__ == '__main__':
37+
try:
38+
main()
39+
except KeyboardInterrupt:
40+
print('Interrupted')
41+
try:
42+
sys.exit(0)
43+
except SystemExit:
44+
os._exit(0)

0 commit comments

Comments
 (0)