RabbitMQ — Hello World Example — by Steven Lacerda (Morgan Hill, CA)
So, I had a problem with the hello world examples I read out on the internet because they all used localhost, and I had set my AMQP server up on a separate server. Thus, here’s the end result of my send and receive of Hello World:
AMQP server is setup on a non-local-host system, and the RabbitMQ docs do a great job explaining how to setup the server, so I’ll direct you to start here for installation and configuation:
https://www.rabbitmq.com/#getstarted
To send, I setup a simple script file called send.py:
1 #!/usr/bin/env python2 import pika34 credentials = pika.PlainCredentials(‘admin’, ‘wizeye1004#’)5 connection = pika.BlockingConnection(pika.ConnectionParameters(‘192.168.0.204’,5672,‘/’,credentials))6 channel = connection.channel()78 channel.queue_declare(queue=‘hello’)9 channel.basic_publish(exchange=‘’, routing_key=‘hello’, body=‘Hello World’)1011 print(” [x] Sent ‘Hello World’“)1213 connection.close()
Then, to receive I setup another script called receive.py:
1 #!/usr/bin/env python2 import pika34 credentials = pika.PlainCredentials(‘admin’, ‘wizeye1004#’)5 connection = pika.BlockingConnection(pika.ConnectionParameters(‘192.168.0.204’,5672,‘/’,credentials))67 channel = connection.channel()89 channel.queue_declare(queue=“hello”)1011 def callback(ch, method, properties, body):12 print(” [x] Received %r” % body)1314 channel.basic_consume(callback, queue=“hello”, no_ack=True)1516 print(' [*] Waiting for messages. To exit press CTRL+C’)17 channel.start_consuming()
And that’s that, you should be able to send and receive, and if you look at your RabbitMQ server queues, you should see a queue called “hello” that’s sending and receiving messages.
Don’t be a prisoner to your code, master it!
Solving a problem is as good as those sexual feelings…sometimes, okay, maybe not. Straight from Morgan Hill, CA. Good luck!
By Steven Lacerda