pythonmqtt

I don't know why but this code sometimes works and sometimes throws an error any idea? :


I've this code that publishes multiple messages to different topics.

""" 
Publish some messages to queue
"""
import paho.mqtt.publish as publish

host ="test.mosquitto.org"

msgs = [{'topic': "A/A1", 'payload': "30"},
        {'topic': "A/A2", 'payload': "40"},
        {'topic': "A/A3", 'payload': "1"}]


if __name__ == '__main__':

    # publish multiple messages
    publish.multiple(msgs, hostname=host)

but some times it works for me and publishes the data and sometimes it throws this error.

Traceback (most recent call last):
  File "/home/pi/Desktop/NRF24L01/publish.py", line 22, in <module>
    publish.multiple(msgs, hostname=host)
  File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/publish.py", line 159, in multiple
    client.connect(hostname, port, keepalive)
  File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 839, in connect
    return self.reconnect()
  File "/usr/local/lib/python3.5/dist-packages/paho/mqtt/client.py", line 962, in reconnect
    sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
  File "/usr/lib/python3.5/socket.py", line 693, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "/usr/lib/python3.5/socket.py", line 732, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] **Temporary failure in name resolution**

after a lot of research I saw some comments saying it's DNS problem or the proxy is closed but couldn't solve in anyway. please any one who can help me? Thanks in advance.


Solution

  • The important part of the error is here:

    socket.gaierror: [Errno -3] **Temporary failure in name resolution**
    

    This means that the Raspberry Pi that you are running the code in could not find the IP address for test.mosquitto.org.

    This most likely means that there is nothing wrong with the code you have, but you need to look at how the Raspberry Pi is connecting to the internet.

    It could be any number of issues but some of them could be:

    Unfortunately there isn't really any way we can know what it is remotely.