#best way is to surround the connection with a try/catch, and set a #timeout for the attempted connection: client.connect(ip,port,username,pwd,timeout=1.5)#timeout is a float