Prasandeep Singh
Prasandeep Singh

Reputation: 65

socket.gaierror gaierror: [Errno -2] Name or service not known - pika rabbitMQ

I am trying to send a message from a python server hosted on localhost:5000 to a RabbitMQ server( using a docker image for RabbitMQ) , but I am getting the following error :

socket.gaierror gaierror: [Errno -2] Name or service not known

I am running the docker image for RabbitMQ using the command where 'rabbithost' is the hostname I am using:

sudo docker run -d --hostname rabbithost --name rabbitmq -p 15672:15672 -p 5672:5672 -p 5671:5671 rabbitmq:3-management

Here is the python code which is giving the error:

def send_to_queue(message):
    credentials = pika.PlainCredentials('guest', 'guest')
    parameters = pika.ConnectionParameters('rabbithost', 5672, '/', credentials)
    connection = pika.BlockingConnection(parameters)
    channel = connection.channel()
    channel.queue_declare(queue='hello')
    channel.basic_publish(exchange='', routing_key='hello',body=message)
    connection.close()
    return "Message Sent!  "

The error is at line :

connection = pika.BlockingConnection(parameters)

mainly because of the parameters argument. I am not able to find the exact solution for this error.

Upvotes: 5

Views: 10142

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146510

Where is the python code running? On localhost? If yes then you either need to change rabbithost to 127.0.0.1 or make a host entry in /etc/hosts for 127.0.0.1 rabbithost

Upvotes: 10

Related Questions