rabbitMQ 接收和发送基于python

  1. 安装rabbitmq  这货是基于elang的 ,要安装语音环境http://erlang.org/download/
  2. 设置用户名,密码
  3. python  用pika
  4. 运行consumer.py
  5. 运行server.py consumer会收到消息”
    演示环境测试测试

consumer.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 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()
View Code

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

 

转载于:https://www.cnblogs.com/malone/p/10530171.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值