RabbitMQ环境搭建好了,接下来就是学习编程的入门级hello world.python
在运行程序前,要先确保开启RabbitMQ服务编程
而后安装pika,命令:pip install pika学习
1.建立一个python工程,我建立的名为RabitMQ_Demo网站
2,建立一个send.pyspa
#!/usr/bin/env python3.5.2 # -*- coding: utf-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) 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()
3,建立一个receive.py.net
#!/usr/bin/env python3.5.2 # -*- coding: utf-8 -*- import pika connection = pika.BlockingConnection(pika.ConnectionParameters( host='localhost')) 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()
4.运行。3d
(1),先运行receive.pycode
(2),而后运行send.pyblog
就此,helloworld就完了,接下来,就去网站上去找教程,加深学习。教程
参考:http://blog.csdn.net/zhangfh1990/article/details/72676411