receive.py 514 B

123456789101112131415161718192021
  1. #!/usr/bin/env python3
  2. import pika
  3. connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
  4. channel = connection.channel()
  5. channel.queue_declare(queue="hello")
  6. def callback(ch, method, properties, body):
  7. # instead of b'Hello World!'
  8. text = body.decode("utf-8")
  9. print(" [x] Received", text)
  10. channel.basic_consume(queue="hello", auto_ack=True, on_message_callback=callback)
  11. print(" [*] Waiting for messages. To exit press CTRL+C")
  12. channel.start_consuming()
  13. connection.close()