ConnectionRefusedError: [Errno 111] Connection refused
Hello, I am using the python paho.mqtt.client to connect with the mosquitto broker installed on the same device. I am getting the connection refused error. The code and the error is like below:
>>> import paho.mqtt.client as mqtt
>>> client = mqtt.Client()
>>> client.connect("192.168.0.13",1883)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 914, in connect
return self.reconnect()
File "/home/user/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "/home/user/.local/lib/python3.9/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/usr/lib/python3.9/socket.py", line 843, in create_connection
raise err
File "/usr/lib/python3.9/socket.py", line 831, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
The mosquitto is loaded and running
$ sudo systemctl status mosquitto
● mosquitto.service - Mosquitto MQTT Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-11-15 11:30:27 +03; 10min ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 2519 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 2520 ExecStartPre=/bin/chown mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 2521 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 2522 ExecStartPre=/bin/chown mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
Main PID: 2523 (mosquitto)
Tasks: 1 (limit: 4643)
Memory: 980.0K
CPU: 691ms
CGroup: /system.slice/mosquitto.service
└─2523 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Nov 15 11:30:27 user systemd[1]: Starting Mosquitto MQTT Broker...
Nov 15 11:30:27 user systemd[1]: Started Mosquitto MQTT Broker.
$ sudo mosquitto -v
1668501697: mosquitto version 2.0.11 starting
1668501697: Using default config.
1668501697: Starting in local only mode. Connections will only be possible from clients running on this machine.
1668501697: Create a configuration file which defines a listener to allow remote access.
1668501697: For more details see https://mosquitto.org/documentation/authentication-methods/
1668501697: Opening ipv4 listen socket on port 1883.
1668501697: Error: Address already in use
1668501697: Opening ipv6 listen socket on port 1883.
1668501697: Error: Address already in use
$ netstat -a | grep 1883
tcp 0 0 localhost:1883 0.0.0.0:* LISTEN
tcp6 0 0 localhost:1883 [::]:* LISTEN
I can connect to the broker by using the pub-sub of the mosquitto-clients and see the messages published so I don't think that something is wrong with broker. But the client somehow doesn't connect to it.
Have you tried using localhost instead of your IP address?
Yes, it doesn't return any errors. But I need this device to work as a bridge between several devices and the client will be handling some processes so I need to make it work with given IP.
1668501697: Opening ipv4 listen socket on port 1883.
1668501697: Error: Address already in use
1668501697: Opening ipv6 listen socket on port 1883.
1668501697: Error: Address already in use
These errors suggest that mosquitto isn't able to open its sockets - perhaps something else is listening already? I don't believe this is a problem with the Python client.

similar error anyone have solution?
Closing due to age/inactivity (and the fact that the original issue appears to have been something else running on the port).