简体   繁体   English

pika rabbitmq python 3.6

[英]pika rabbitmq python 3.6

I am trying to use pika to connect with rabbitmq我正在尝试使用 pika 连接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 一起使用,输出如下所示:

# 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.我还可以在 rabbitmq 端口上进行 telnet 访问,并且用户可以访问该频道。

When execute the python code I get this error:执行python代码时出现此错误:

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.在我credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)即使在下一行出现错误时,这行credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)发生一些事情credentials = pika.PlainCredentials(MQ_USER, MQ_PASS) 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:编辑:我说我认为错误是在这行credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)因为如果我添加如下内容:

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.因此,我还尝试用实际值替换,如credentials = pika.PlainCredentials('user', 'mq@pass')并得到相同的结果。

EDIT2: Answering to the comments bellow. EDIT2:回答下面的评论。

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. Rabbit MQ 在远程 IP 上运行。 I already tested and I can telnet to the IP.我已经测试过了,我可以 telnet 到 IP。

pika.exceptions.AMQPConnectionError is raised when the host is not reachable by pika.当 pika 无法访问host时,会引发pika.exceptions.AMQPConnectionError

In case of invalid credentials, pika raises:如果凭据无效,pika 会引发:

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.检查提供的hostport值是否有效。

Reference: pika docs参考: pika 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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