2017-10-17 90 views
0

我試圖從一臺機器連接到另一臺安裝了RabbitMQ的遠程服務器。 所述的RabbitMQ是可以正常使用局部,但是當我連接到它從另一臺機器然後的誤差發生:爲什麼Python代碼無法遠程連接到RabbitMQ?

[email protected]:~# python3 rabbitmq.py 
Traceback (most recent call last): 
    File "rabbitmq.py", line 8, in <module> 
    connection = pika.BlockingConnection(pika.ConnectionParameters(parameters)) 
    File "/usr/local/lib/python3.4/dist-packages/pika/connection.py", line 652, in __init__ 
    self.host = host 
    File "/usr/local/lib/python3.4/dist-packages/pika/connection.py", line 392, in host 
    (value,)) 
TypeError: host must be a str or unicode str, but got <ConnectionParameters host=111.111.111.111 port=5672 virtual_host=product ssl=False> 
[email protected]:~# 

TypeError: host must be a str or unicode str, but got ConnectionParameters host=111.111.111.111 port=5672 virtual_host=product ssl=False

其它遠程機器上

的Python代碼:

import pika 
credentials = pika.PlainCredentials(username='remoteuser', password='mypassword') 
parameters = pika.ConnectionParameters(host='111.111.111.111', port=5672, virtual_host='/', credentials=credentials) 
#connection = pika.BlockingConnection(pika.ConnectionParameters('111.111.111.111:15672')) # --- it doesn't work too 
connection = pika.BlockingConnection(pika.ConnectionParameters(parameters)) 
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() 

用戶「remoteuser」具有管理權限並訪問虛擬主機「/」

http://111.111.111.111:15672/#/users

Name  Tags   Can access virtual hosts Has password 
remoteuser administrator /        ● 

什麼問題?

回答

1

你有雙重包裹parameters,變化:

connection = pika.BlockingConnection(pika.ConnectionParameters(parameters)) 

到:

connection = pika.BlockingConnection(parameters) 
+0

是的!有用!非常感謝你! – Sergey

+0

很高興幫助。 – georgexsh