生產者
import pika
#coding=utf8
credentials = pika.PlainCredentials('guest', '密碼')
connection = pika.BlockingConnection(pika.ConnectionParameters('IP',5672,'/',credentials))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='rabbitmq test!')
print("開始隊列")
connection.close()
消費者
import pika
#coding=utf8
credentials = pika.PlainCredentials('guest', '密碼')
connection = pika.BlockingConnection(pika.ConnectionParameters('IP',5672,'/',credentials))
channel = connection.channel()
# rabbitmq消費端仍然使用此方法創建隊列。這樣做的意思是:若是沒有就創建。和發送端道理道理。目的是為了保證隊列一定會有
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)
print(' [*] Waiting for messages.')
channel.start_consuming()
