RabbitMQ做外AMQP的实现,现在在很多企业中作为消息中心在运用。
RabbitMQ在Python中,采用pika工具进行操作。在之前的版本里,也有采用py-amqplib工具,但后面几乎不再采用。
以下是笔者的一两个小实验。
getMessage.py
#!/usr/bin/env python import pika import random credentials = pika.PlainCredentials('crawler', 'Abc12345') parameters = pika.ConnectionParameters('192.168.161.110',5672,'crawler',credentials) connection = pika.BlockingConnection(parameters) channel = connection.channel() print ' [*] Waiting for messages. To exit press CTRL+C' def callback(ch, method, properties, body): print " [x] Received %r" % (body,) channel.basic_consume(callback,queue='taobao.policy',no_ack=True) channel.start_consuming()
sendMessage.py
扫描二维码关注公众号,回复:
549408 查看本文章
#!/usr/bin/env python import pika import random credentials = pika.PlainCredentials('crawler', 'Abc12345') parameters = pika.ConnectionParameters('192.168.161.110',5672,'crawler',credentials) connection = pika.BlockingConnection(parameters) channel = connection.channel() print ' [*] Waiting for messages. To exit press CTRL+C' def callback(ch, method, properties, body): print " [x] Received %r" % (body,) channel.basic_consume(callback,queue='taobao.policy',no_ack=True) channel.start_consuming()
References:
http://pika.readthedocs.org/en/0.9.14/