My python script turns on the Raspberry Pi's WiFi and after a short delay tries to connect with Paho MQTT. If the delay isn't long enough, it fails ungracefully.

I'm using paho-mqtt version 1.4.1

EDIT: I've added the line that starts status_con = which should have been present already. I do indeed instantiate the client first, then use .connect() immediately afterward. It's the .connect() that produces the error.


    mqtt_client = mqtt.Client(client_id="Luke, I am your client",
                              clean_session=True, userdata=None, 

    status_con  = self.mqtt_client.connect(host=self.mqtt_host_url,
                                           port=self.mqtt_port)  # 5 https://stackoverflow.com/a/35581280/3904031

except:  # never gets here!

    print "connect failed, sleeping for 20 seconds: "
    print "    now try again: "
    mqtt_client = mqtt.Client(client_id="Luke, I'm no longer your client",
                              clean_session=True, userdata=None, 

Instead of being caught by the try: it errors and gives the following messages

File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 768, in connect
  return self.reconnect()

File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 895, in reconnect
  sock = socket.create_connection((self._host, self._port), 
  source_address=(self._bind_address, 0))

File "/usr/lib/python2.7/socket.py", line 553, in create_connection
  for res in getaddrinfo(host, port, 0, SOCK_STREAM):gaierror: 
  [Errno -5] No address associated with hostname

If I increase the delay, then it always connects.

Right now my solution is to ping a random internet site every few seconds using socket until it's working, then trying to connect MQTT but I would still like something that doesn't have such catastrophic results.

So what I would like is for a way to catch the problem without Paho crashing, and only then try again in 10 seconds, and again...

Is there a way to have Paho do this for me, or at least to let me handle the exception with try/except?

  • 1
    The comment about increasing the delay, do you mean the time.sleep(20)? because there is no try/except round the second connect.
    – hardillb
    Dec 11 '18 at 17:38
  • 1
    You probably want the try in a loop, so that exceptions are caught every time. Dec 11 '18 at 20:22
  • @hardillb the posted script is not a solution, It was a quick test which showed that the try didn't even help the first time; and the print "connect failed... was never executed. If it had, I could manage the situation. About delay, no, I mean the delay described in the first two sentences.
    – uhoh
    Dec 11 '18 at 20:39
  • @GambitSupport see comment above. If I could get the except to work, then I'd use a loop and call it a solution, albeit not pretty or permanent.
    – uhoh
    Dec 11 '18 at 20:44
  • 1
    found this .... looks like attempting the connection separately, after the client object is created, may be the answer .... steves-internet-guide.com/client-connections-python-mqtt
    – jsotola
    Dec 16 '18 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.