'How to connect to rabbit on vagrant host?

I set up a server using vagrant on a virtual machine. After installing rabbitmq, I tried to connect to it using script outside VM. There's already Django and RabbitMQ running on VM. 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. 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.

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',)


Solution 1:[1]

@Pigeonnn provided the answer to his own question in his own comment to the original question on this very post:

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 :)

Solution 2:[2]

first forward the a host port to a guest port on Vagrant in the vagrant configuration file (Vagrantfile). 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.

Solution 3:[3]

Stating the docs and highlighting the response, RabbitMQ listening ports are:

AMQP:       5672
AMQP/ssl:   5671
HTTP management UI: 15672

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 essoh leonel
Solution 3 dejanualex