【发布时间】:2017-09-13 01:50:13
【问题描述】:
我正在尝试将消息从托管在 localhost:5000 上的 python 服务器发送到 RabbitMQ 服务器(使用 RabbitMQ 的 docker 映像),但我收到以下错误:
socket.gaierror gaierror: [Errno -2] Name or service not known
我正在使用命令运行 RabbitMQ 的 docker 映像,其中“rabbithost”是我正在使用的主机名:
sudo docker run -d --hostname rabbithost --name rabbitmq -p 15672:15672 -p 5672:5672 -p 5671:5671 rabbitmq:3-management
这是给出错误的python代码:
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! "
错误在行:
connection = pika.BlockingConnection(参数)
主要是因为参数参数。 我无法找到此错误的确切解决方案。
【问题讨论】:
-
python代码在哪里运行?在本地主机上?如果是,那么您要么需要将
rabbithost更改为127.0.0.1,要么在/etc/hosts中为127.0.0.1 rabbithost创建一个主机条目 -
@TarunLalwani 这确实有效! .谢谢你的建议。
标签: python sockets docker rabbitmq pika