码迷,mamicode.com
首页 > 编程语言 > 详细

第五章:Python 之 RabbitMQ 基本示例

时间:2017-10-25 23:37:58      阅读:166      评论:0      收藏:0      [点我收藏+]

标签: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 基本示例

标签:rabbitmq

原文地址:http://mingkang.blog.51cto.com/9678221/1976076

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!