简体   繁体   中英

pika rabbitmq python 3.6

I am trying to use pika to connect with rabbitmq

def get_connection():
    credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)
    connection = pika.BlockingConnection(pika.ConnectionParameters(MQ_SERVER, 5672, '/', credentials))
    return connection

I can use those credentials with rabbitmqctl, the output is something like this:

# rabbitmqctl authenticate_user user pass
Authenticating user "user" ...
Success

I have also tried to just use strings with the values inside the function and get the same error. I also have telnet access on the rabbitmq port and the user has access to the channel.

When execute the python code I get this error:

Internal Server Error: /api/analysis/stream/finish/
Traceback (most recent call last):
  File "/path/to/api/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/path/to/api/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/path/to/api/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/path/to/api/core/views.py", line 2465, in record_finsh
    inform_process(video.filename)
  File "/path/to/api/core/views.py", line 702, in inform_process
    con = get_connection()
  File "/path/to/api/base/rabitmq.py", line 7, in get_connection
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=MQ_SERVER, port=5672, virtual_host='/', credentials=credentials))
  File "/path/to/api/venv/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
    self._impl = self._create_connection(parameters, _impl_class)
  File "/path/to/api/venv/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
    raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError

It looks to me like something happens on this line credentials = pika.PlainCredentials(MQ_USER, MQ_PASS) even when the error in on the next line. What does this function do exactly? Any ideas of what I am doing wrong?

EDIT: I said I think the error is on this line credentials = pika.PlainCredentials(MQ_USER, MQ_PASS) because if I add something like:

def get_connection():
    credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)
    exit()
    connection = pika.BlockingConnection(pika.ConnectionParameters(MQ_SERVER, 5672, '/', credentials))
    return connection

I still get more or less the same error:

Internal Server Error: /api/analysis/stream/finish/
Traceback (most recent call last):
  File "/path/to/api/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/path/to/api/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/path/to/api/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/path/to/api/core/views.py", line 2465, in record_finsh
    inform_process(video.filename)
  File "/path/to/api/core/views.py", line 702, in inform_process
    con = get_connection()
  File "/path/to/api/base/rabitmq.py", line 7, in get_connection
    return 0
  File "/path/to/api/venv/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 360, in __init__
    self._impl = self._create_connection(parameters, _impl_class)
  File "/path/to/api/venv/lib/python3.6/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
    raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError

Because of this I also tried replacing with actual values like credentials = pika.PlainCredentials('user', 'mq@pass') and also get the same result.

EDIT2: Answering to the comments bellow.

def get_connection():
    credentials = pika.PlainCredentials('user', 'mq@passwd')
    connection = pika.BlockingConnection(pika.ConnectionParameters('172.x.y.z', 5672, '/', credentials))
    return connection

Returns the same issue. Rabbit MQ runs on remote IP. I already tested and I can telnet to the IP.

pika.exceptions.AMQPConnectionError is raised when the host is not reachable by pika.

In case of invalid credentials, pika raises:

pika.exceptions.ConnectionClosedByBroker: (403, 'ACCESS_REFUSED - Login was refused using authentication mechanism PLAIN. For details see the broker logfile.')

for invalid virtual host:

pika.exceptions.ConnectionClosedByBroker: (530, 'NOT_ALLOWED - vhost / not found')

Check if the host or port value provided is valid.

Reference: pika docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM