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