0

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)
Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • Where is your rabbitmq instance running? Docker or actually local? – pedromcaraujo Feb 07 '22 at 15:39
  • @pedromcaraujo It runs in docker. It makes sense now :facepalm:. Still, I didn't see the tutorial telling how to connect to a non localhost rabbit – Gulzar Feb 07 '22 at 15:42
  • Rabbitmq's tutorial assumes you have a rabbit running in localhost without any credentials or anything (which is usually not the case). Try something like BlockingConnection(pika.URLParameters("amqp://guest:guest@localhost/")). If your docker is well configured you should be able to reach it through localhost – pedromcaraujo Feb 07 '22 at 15:48
  • @pedromcaraujo I tried again with parameters that should fit the running docker. How to debug what's missing? Please see edit – Gulzar Feb 07 '22 at 16:21
  • [This](https://stackoverflow.com/questions/71033283/using-pika-how-to-connect-to-rabbitmq-running-in-docker-started-with-docker-co) is the answer. Problem was a docker network I had to connect to. – Gulzar Feb 08 '22 at 17:47

0 Answers0