I am following the official hello-world tutorial in pika:
Here is copy-pasted code from the tutorial
import pika
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
which fails on the very first line connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
with
File "...", line 5, in <module>
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
File ".../venv/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "/.../venv/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError
What's wrong with my setup and how to get the library going?
EDIT
rabbitmq runs in a docker: docker ps
gives
bb6b0e80846e rabbitmq:3.6-management-alpine "docker-entrypoint.s…" 22 seconds ago Up 21 seconds 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, :::5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp, :::15672->15672/tcp rabbitmq
The following
credentials = pika.credentials.PlainCredentials(
username="guest",
password="guest",
)
parameters = pika.ConnectionParameters(
host="rabbitmq",
port="5672",
credentials=credentials,
heartbeat=10,
)
pika.BlockingConnection(parameters)
Getting
connection: pika.BlockingConnection = pika.BlockingConnection(parameters)
File ".../venv/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File ".../venv/lib/python3.8/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
raise self._reap_last_connection_workflow_error(error)
File ".../venv/lib/python3.8/site-packages/pika/adapters/utils/selector_ioloop_adapter.py", line 565, in _resolve
result = socket.getaddrinfo(self._host, self._port, self._family,
File "/usr/local/lib/python3.8/socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
Also happens with
rabbit_container = client.containers.get("rabbitmq")
ip_address = rabbit_container.attrs["NetworkSettings"]["Networks"][network_name]["IPAddress"]
parameters = pika.ConnectionParameters(
host=ip_address,
port=rmq_connection_params.port,
credentials=credentials,
heartbeat=10,
)
connection: pika.BlockingConnection = pika.BlockingConnection(parameters)