Python&Rabbitmq

2019-04-19  本文已影响0人  Donald_32e5

一、类库

二、基本使用

三、Hello World

发送消息

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
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()

接受消息

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

# 确认队列
channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

# 接收消息
channel.basic_consume('hello', callback, auto_ack=True)

channel.start_consuming()

四、注意

上一篇下一篇

猜你喜欢

热点阅读