python rabitmq_利用python测试Rabbitmq消息发送和接收功能

Tip:

此文前提是rabbitmq已经安装。本文只测试rabbitmq的功能

系统:

Centos 6.8 x64

一、安装python依赖组件

yum -y install wget setuptool

安装pip

cd /tmp

wget https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz#md5=01026f87978932060cc86c1dc527903e

tar zxvf pip-1.5.6.tar.gz

cd pip-1.5.6

python setup.py install

安装pika

cd /tmp

pip install --upgrade pip setuptools

wget https://pypi.python.org/packages/source/p/pika/pika-0.9.14.tar.gz#md5=b99aad4b88961d3c7e4876b8327fc97c

tar zxvf pika-0.9.14.tar.gz

cd pika-0.9.14

python setup.py install

二、测试rabbitmq功能

cd /tmp

下面是发送消息脚本:

cat send.py

# coding:utf-8

import pika

username = 'jst'

password = 'jsdst'

host = '10.101.130.25'

credentials = pika.PlainCredentials(username, password)

connection = pika.BlockingConnection(pika.ConnectionParameters(host=host, credentials=credentials, port=5672))

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

运行发送脚本

python send.py

下面是接收消息脚本:

cat rec.py

# coding:utf-8

import pika

username = 'jst'

password = 'jsdst'

host = '10.101.130.25'

credentials = pika.PlainCredentials(username, password)

connection = pika.BlockingConnection(pika.ConnectionParameters(

host=host, credentials=credentials, port=5672

))

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

运行接收脚本

python rec.py

登录rabbitmq查看是否成功,略。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值