Project - RabbitMQ

Python Programs

Receive

# ==================================================================
# RabbitMQ test - receiver
#
# From: www.rabbitmq.com/tutorials/tutorial-one-python.html
# ==================================================================

import pika

connection = pika.BlockingConnection( \
        pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

def callback(ch,method,proberties,body):
    print(' [x] received %r' % body)

channel.basic_consume(callback, \
        queue='hello', \
        no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL-C')
channel.start_consuming()

Send

# ==================================================================
# RabbitMQ test - sender
#
# From: www.rabbitmq.com/tutorials/tutorial-one-python.html
# ==================================================================

import pika
import sys

connection = pika.BlockingConnection( \
        pika.ConnectionParameters(host='localhost'))
channel = connection.channel()

channel.exchange_declare(exchange='hello')

message = ' '.join(sys.argv[1:]) or "Hello World!"

channel.basic_publish(exchange='', \
        routing_key='hello',       \
        body=message)

print(" [x] Sent %r" % message)

connection.close()