rabbitMQ 接收和發送基於python

  1. 安裝rabbitmq  這貨是基於elang的 ,要安裝語音環境http://erlang.org/download/
  2. 設置用戶名,密碼
  3. python  用pika
  4. 運行consumer.py
  5. 運行server.py consumer會收到消息」
    演示環境測試測試

consumer.pypython

 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.pyide

 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()
相關文章
相關標籤/搜索