paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

socket.error: [Errno 111] Connection refused

Open sergge1 opened this issue 5 years ago • 3 comments

Hello.

I have eclipse-mosquitto as a mqtt broker and paho-mqtt as a mqtt client installed at the same box, which is Raspberry Pi 3 B+.

Current behavior: The client paho.mqtt.python ended with socket.error no.111. if my broker is unreachable (just did not started yet prior to the client).

Expected behavior: Client should make attempts to reconnect until the broker did not become active/reachable.

Traceback

pi@raspberrypi:~/mqtt_clients $ sudo python pir.py
Script started. PIR sensor is activated. Looking for a movement...
Traceback (most recent call last):
  File "pir.py", line 59, in <module>
    main()
  File "pir.py", line 34, in main
    motion_off()
  File "pir.py", line 46, in motion_off
    client.connect(BROKER_ADDRESS, BROKER_PORT)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 937, in connect
    return self.reconnect()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1071, in reconnect
    sock = self._create_socket_connection()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 3522, in _create_socket_connection
    return socket.create_connection(addr, source_address=source, timeout=self._keepalive)
  File "/usr/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 111] Connection refused

sergge1 avatar Jan 11 '20 09:01 sergge1

I was also getting this similar issue. But for me everything was working fine and suddenly one fine day it started throwing this error. First make sure you can connect to your broker normally from other test clients.

I resolve it by changing transport to websockets. https://github.com/eclipse/paho.mqtt.python/issues/467#issuecomment-593561726

bkrajendra avatar Mar 02 '20 19:03 bkrajendra

I also have the same issue. I don't know how to let the script keep retrying until a connection has been stablished.

wonx avatar May 15 '21 19:05 wonx

I had the issue described in the Current behavior section. Tried websockets as suggested but that just caused different issues.

In my case, switching from Python 2.7 to 3.7 fixed the issue; on 3.7 it works with both tcp and websockets transports.

Likely works on newer versions of 3.x as well but haven't tested.

martopg avatar Feb 13 '22 17:02 martopg

I'm going to close this due to it's age/inactivity (sorry that no-one responded) and the fact that the next release will only support Python 3.7+ (which comments above indicate may resolve the issue). In addition support for socket.error was removed back in August 2021.

If you are still experiencing this issue (with the current release and Python 3.7+) then please feel free to reopen. Unfortunately, as you will note from the lack of response, we have limited resources so will not be able to provide support for old python versions.

MattBrittan avatar Jan 07 '24 22:01 MattBrittan