eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.17k stars 723 forks source link

Exception ConnectionRefusedError for non valid mqtt host #663

Closed pavkamlc closed 1 month ago

pavkamlc commented 2 years ago

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)

#mqttc.subscribe("test", 0)
print(rc)
print('connect event')

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()_

pavkamlc commented 2 years ago

Code tested on python 3.10.4 on Win10, paho-mqtt 1.6.1

ralight commented 1 year ago

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.

MattBrittan commented 1 month ago

Closing due to age/inactivity/need for additional info. Please feel free to reopen (with further info) if this is still an issue.