簡體   English   中英

pika rabbitmq python 3.6

[英]pika rabbitmq python 3.6

我正在嘗試使用 pika 連接rabbitmq

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

我可以將這些憑據與 rabbitmqctl 一起使用,輸出如下所示:

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

我還嘗試僅使用帶有函數內部值的字符串並得到相同的錯誤。 我還可以在 rabbitmq 端口上進行 telnet 訪問,並且用戶可以訪問該頻道。

執行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

在我credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)即使在下一行出現錯誤時,這行credentials = pika.PlainCredentials(MQ_USER, MQ_PASS)發生一些事情credentials = pika.PlainCredentials(MQ_USER, MQ_PASS) 這個函數具體是做什么的? 關於我做錯了什么的任何想法?

編輯:我說我認為錯誤是在這行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

我仍然或多或少地遇到相同的錯誤:

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

因此,我還嘗試用實際值替換,如credentials = pika.PlainCredentials('user', 'mq@pass')並得到相同的結果。

EDIT2:回答下面的評論。

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

返回相同的問題。 Rabbit MQ 在遠程 IP 上運行。 我已經測試過了,我可以 telnet 到 IP。

當 pika 無法訪問host時,會引發pika.exceptions.AMQPConnectionError

如果憑據無效,pika 會引發:

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

對於無效的虛擬主機:

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

檢查提供的hostport值是否有效。

參考: pika 文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM