第五章:Python 之 RabbitMQ 基本示例

#send 端

import pika

credentials = pika.PlainCredentials(
'root', 'Password1')

connection = pika.BlockingConnection(pika.ConnectionParameters(
'10.3.151.86',5672,'/',credentials))

channel = connection.channel()          
#通過connection實例創建一個channel管道

channel.queue_declare(queue='hello')    #在管道中創建一個隊列

channel.basic_publish(exchange='',routing_key='hello',body='Hello Wfffforld!')

connection.close()

#receive 端
import pika

credentials = pika.PlainCredentials(
'root', 'Password1')

connection = pika.BlockingConnection(pika.ConnectionParameters(
'10.3.151.86',5672,'/',credentials))

channel = connection.channel()

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. To exit press CTRL+C')

channel.start_consuming()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章