1. 程式人生 > >第五章:Python 之 RabbitMQ 基本示例

第五章:Python 之 RabbitMQ 基本示例

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()


本文出自 “學習旅程” 博客,請務必保留此出處http://mingkang.blog.51cto.com/9678221/1976076

第五章:Python 之 RabbitMQ 基本示例