rabbitmq01 简单的hello world!

2018-08-26  本文已影响0人  6c0fe9142f09

课前准备:https://www.jianshu.com/p/d6e9309535b7(单节点安装部分)

简单的hello world!

1.发送消息
pip install pika==0.9.5
import pika
# 创建一个连接器
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
# 建立一个channel
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', # 交换机
                      routing_key='hello',  # 队列
                      body='Hello World!')  # 消息
print " [x] Sent 'Hello World!'"
connection.close()
./rabbitmqctl list_queues name messages_ready messages_unacknowledged 
2.获取消息
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True) # 暂时先不用管
上一篇 下一篇

猜你喜欢

热点阅读