为什么Python代码无法远程连接到RabbitMQ?

问题描述:

我试图从一台机器连接到另一台安装了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 /        ● 

什么问题?

你有双重包裹parameters,变化:

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

到:

connection = pika.BlockingConnection(parameters) 
+0

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

+0

很高兴帮助。 – georgexsh