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

时间:2017-10-17 15:38:14

标签: python rabbitmq remote-access

我正在尝试使用已安装的RabbitMQ从一台计算机连接到另一台远程服务器。 RabbitMQ在本地完美运行,但是当我从另一台机器连接到它时,则会发生错误:

root@xxx:~# 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>
root@xxx:~#
  

TypeError:host必须是str或unicode str,但是得到了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 个答案:

答案 0 :(得分:3)

你有双重包裹parameters,更改:

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

为:

connection = pika.BlockingConnection(parameters)