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

 

posted @ 2019-03-14 14:37  Edwinma  阅读(438)  评论(0编辑  收藏  举报