Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect pika to rabbitMQ remote server? (python, pika)

In my local machine I can have:

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))

for both scripts (send.py and recv.py) in order to establish proper communication, but what about to establish communication from 12.23.45.67 to 132.45.23.14 ? I know about all the parameters that ConnectionParameters() take but I am not sure what to pass to the host or what to pass to the client. It would be appreciated if someone could give an example for host scrip and client script.

like image 684
Eriel Marimon Avatar asked Jan 06 '15 18:01

Eriel Marimon


People also ask

How do I connect to my RabbitMQ remote server?

Create new RabbitMQ user and set permissions To create a new RabbitMQ user to access the RabbitMQ server remotely: Open a browser and navigate to http://localhost:15672/. The RabbitMQ Management login screen displays. Log into RabbitMQ using guest as both the username and password.

How do I connect to RabbitMQ server in Python?

Setting up rabbitmq-server But first we need to install “rabbitmq-server” which will run as a system program at backend. sudo rabbitmqctl set_permissions -p / user "." "." "." This will start the rabbitmq-server. And you are now all set for accessing it using an AMQP rabbitmq client called pika in python.


1 Answers

first step is to add another account to your rabbitMQ server. To do this in windows...

  1. open a command prompt window (windows key->cmd->enter)
  2. navigate to the "C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\sbin" directory ( type "cd \Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\sbin" and press enter )
  3. enable management plugin (type "rabbitmq-plugins enable rabbitmq_management" and press enter)
  4. open a broswer window to the management console & navigate to the admin section (http://localhost:15672/#/users with credentials "guest" - "guest")
  5. add a new user (for example "the_user" with password "the_pass"
  6. give that user permission to virtual host "/" (click user's name then click "set permission")

Now if you modify the connection info as done in the following modification of send.py you should find success:

#!/usr/bin/env python
import pika

credentials = pika.PlainCredentials('the_user', 'the_pass')
parameters = pika.ConnectionParameters('132.45.23.14',
                                   5672,
                                   '/',
                                   credentials)

connection = pika.BlockingConnection(parameters)

channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                  routing_key='hello',
                  body='Hello W0rld!')
print(" [x] Sent 'Hello World!'")
connection.close()

Hope this helps

like image 190
HyperActive Avatar answered Sep 17 '22 14:09

HyperActive