Files
python3-cookbook/basic/pika/receivelog.py
2014-09-02 04:46:28 +08:00

19 lines
693 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='logs')
result = channel.queue_declare(queue='logqueue')
queue_name = result.method.queue
# 新建一个queue并且把它绑定到一个exchange上面并且指定routing_key
channel.queue_bind(exchange='logs', routing_key='log', queue=queue_name)
print('[*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print("[x] %r" % (body,))
# 这个就是直接消费一个queue了
channel.basic_consume(callback, queue=queue_name, no_ack=True)
channel.start_consuming()