- 安装rabbitmq 这货是基于elang的 ,要安装语音环境http://erlang.org/download/
- 设置用户名,密码
- python 用pika
- 运行consumer.py
- 运行server.py consumer会收到消息”
演示环境测试测试
“
consumer.py
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 import pika 2 3 credentials = pika.PlainCredentials('pymq','123123') 4 connection = pika.BlockingConnection(pika.ConnectionParameters( 5 '10.1.120.131',5672,'test_host',credentials)) 6 channel = connection.channel() 7 8 channel.queue_declare(queue='balance') 9 10 11 def callback(ch, method, properties, body): 12 print(" [x] Received %r" % body) 13 print(bytes.decode(body)) 14 15 16 channel.basic_consume(callback, 17 queue='balance', 18 no_ack=True) 19 20 print(' [*] Waiting for messages. To exit press CTRL+C') 21 channel.start_consuming()
server.py
1 import pika 2 3 credentials = pika.PlainCredentials('pymq', '123123') 4 connection = pika.BlockingConnection(pika.ConnectionParameters( 5 '10.1.120.131', 5672, 'test_host', credentials 6 )) 7 channel = connection.channel() 8 9 #queue 10 channel.queue_declare(queue='balance') 11 12 channel.basic_publish( 13 exchange='', 14 routing_key='balance', 15 body='演示环境测试测试') 16 17 connection.close()