Closed pavkamlc closed 1 month ago
Code tested on python 3.10.4 on Win10, paho-mqtt 1.6.1
I'm sorry, I don't quite understand. It sounds like you are saying that you don't want a ConnectionRefusedError to be raised if you use an invalid host, is that correct? Or that you want the library to handle that for you? The behaviour you describe as not working looks correct to me.
Closing due to age/inactivity/need for additional info. Please feel free to reopen (with further info) if this is still an issue.
Is't possible to catch exception for refused connection? I test application without correct mqtt information because in production is possible bad configuration. Then I tried catch exception with connect, but it seems be imposible. I always get excepption ConnectionRefusedError [WinError 10061] on line client1.connect(...) I don't test behaviour with correct configuration, but it will be another scenario.
_client1 = paho.Client() # create client object
ret = 0
def on_disconnect(client, userdata, rc): print("client disconnected ok")
def on_publish(client, userdata, result): # create function for callback print("data published \n") pass
def on_connect(mosq, userdata, flags, rc):
raise(MoreTrouble)
def on_message(client, userdata, msg): try: print(msg) except: print('message event')
client1.on_publish = on_publish # assign function to callback client1.on_disconnect = on_disconnect client1.on_connect = on_connect client1.on_message = on_message
try: client1.connect(config['mqtt_host'], config['mqtt_port']) # establish connection while True: print('Publish topic') ret = client1.publish("house/bulb1", "on") # publish time.sleep(1)
except (Exception, ConnectionError, ConnectionRefusedError): print("stop") client1.disconnect()
client1.disconnect()_