[英]How to connect to rabbit on vagrant host?
I set up a server using vagrant on a virtual machine.我在虚拟机上使用 vagrant 设置了一个服务器。 After installing rabbitmq, I tried to connect to it using script outside VM.安装 rabbitmq 后,我尝试使用 VM 外部的脚本连接到它。 There's already Django and RabbitMQ running on VM. VM 上已经运行了 Django 和 RabbitMQ。 After running a script I have an exception:运行脚本后出现异常:
pika.exceptions.IncompatibleProtocolError: StreamLostError: ('Transport indicated EOF',)
How to solve my problem?如何解决我的问题?
My friend already used the code provided below on raspberryPi which actually managed to execute it.我的朋友已经在 raspberryPi 上使用了下面提供的代码,它实际上成功地执行了它。 The only thing I changed on my PC was the hostname changed from the specified IP to my '127.0.0.1'and I added the port number.我在 PC 上唯一更改的是主机名从指定的 IP 更改为我的“127.0.0.1”,并且我添加了端口号。
import pika
import sys
import random
import time
credentials = pika.PlainCredentials(username='admin', password='admin')
connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1',port=15672,credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='hello',durable=True)
Error message:错误信息:
$ python send.py
Traceback (most recent call last):
File "send.py", line 8, in <module>
connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1',port=15672,credentials=credentials))
File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\pika\adapters\blocking_connection.py", line 360, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "C:\Users\Pigeonnn\AppData\Local\Programs\Python\Python37\lib\site-packages\pika\adapters\blocking_connection.py", line 451, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.IncompatibleProtocolError: StreamLostError: ('Transport indicated EOF',)
@Pigeonnn provided the answer to his own question in his own comment to the original question on this very post: @Pigeonnn 在他自己对这篇帖子的原始问题的评论中提供了他自己问题的答案:
Actually I've just found a solution.其实我刚刚找到了一个解决方案。 The thing is if you want to listen to rabbitmq you need to connect through port 5672 - not 15672. Changed ports, forwarded and everything works :)问题是如果你想听 rabbitmq,你需要通过端口 5672 - 而不是 15672 连接。更改端口,转发,一切正常:)
first forward the a host port to a guest port on Vagrant in the vagrant configuration file (Vagrantfile).首先在 vagrant 配置文件(Vagrantfile)中将主机端口转发到 Vagrant 上的访客端口。 Beware to not utilise a host port that is already used.请注意不要使用已使用的主机端口。
Vagrant.configure("2") do |config|
config.vm.network "forwarded_port", guest: 5672, host: 5671 # Rabbit
end
then connect like so:然后像这样连接:
credentials = pika.PlainCredentials(username='admin', password='admin')
connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1',port=5671,credentials=credentials))
don't forget to configure the user admin accordingly.不要忘记相应地配置用户管理员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.