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查看是否成功,略。