我在Ubuntu 14.04上安装了RabbitMQ 3.6.9,我想用pika制作一个基本的生产者 - 消费者示例,但与RabbitMQ网站上的教程不同,我喜欢消费者要在另一台机器上。
我知道我可以将[{rabbit, [{loopback_users, []}]}].
添加到rabbitmq.config并避免所有身份验证问题,但我想在我的示例中包含连接的身份验证方面。
我首先添加了一个新用户:rabbitmqctl add_user user password
,然后运行了以下脚本:
import pika
url = '10.0.0.1'
credentials = pika.PlainCredentials('user', 'password')
parameters = pika.ConnectionParameters(host=url, credentials=credentials)
connection = pika.BlockingConnection(parameters)
connection.close()
print 'Made it through!'
但是,我得到以下例外:
Traceback (most recent call last):
File "test.py", line 8, in <module>
connection = pika.BlockingConnection(parameters)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 339, in __init__
self._process_io_for_connection_setup()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 374, in _process_io_for_connection_setup
self._open_error_result.is_ready)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py", line 410, in _flush_output
self._impl.ioloop.poll()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/select_connection.py", line 602, in poll
self._process_fd_events(fd_event_map, write_only)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/select_connection.py", line 443, in _process_fd_events
handler(fileno, events, write_only=write_only)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 364, in _handle_events
self._handle_read()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 407, in _handle_read
return self._handle_error(error)
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 338, in _handle_error
self._handle_disconnect()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 288, in _handle_disconnect
self._adapter_disconnect()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/select_connection.py", line 95, in _adapter_disconnect
super(SelectConnection, self)._adapter_disconnect()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 154, in _adapter_disconnect
self._check_state_on_disconnect()
File "/usr/local/lib/python2.7/dist-packages/pika/adapters/base_connection.py", line 178, in _check_state_on_disconnect
raise exceptions.ProbableAccessDeniedError
pika.exceptions.ProbableAccessDeniedError
根据pika的消息来源,这意味着:&#34;客户端在连接阶段断开连接,表明可能拒绝访问指定的虚拟主机&#34;,所以我想也许我应该添加一个虚拟主机而不是使用默认值,所以我运行rabbitmqctl add_vhost testvhost
,重新启动服务器,并运行以下脚本:
import pika
url = '10.0.0.1'
vhost = 'testhvost'
credentials = pika.PlainCredentials('user', 'password')
parameters = pika.ConnectionParameters(host=url, virtual_host=vhost, credentials=credentials)
connection = pika.BlockingConnection(parameters)
connection.close()
print 'Made it through!'
但是我仍然得到同样的例外。
我错过了什么?在我能让它工作之前,我需要有任何基本的rabbitmq.config文件吗?服务器是一个干净的安装,因此目前没有加载配置文件。
答案 0 :(得分:1)
您是否向该用户添加了访问虚拟主机的权限?
到默认虚拟主机:
rabbitmqctl set_permissions user ".*" ".*" ".*"
到你新创建的:
rabbitmqctl set_permissions -p /testhvost user ".*" ".*" ".*"