What will be the output when this Python MQTT client code tries to connect to a broker that is offline?
import paho.mqtt.client as mqtt client = mqtt.Client() result = client.connect('localhost', 1883, 60) print(result)
Check the meaning of return codes from connect() in paho-mqtt.
The connect() method returns 0 on success. If the broker is offline, it returns 1 indicating connection refused.
Which option correctly sets the keepalive interval to 120 seconds when connecting with paho-mqtt client?
import paho.mqtt.client as mqtt client = mqtt.Client() client.connect('broker.hivemq.com', 1883, ???)
Look at the connect() method signature for the keepalive parameter position.
The third parameter of connect() is the keepalive interval in seconds. So passing 120 as the third argument sets it correctly.
What is the correct sequence of MQTT client callbacks to receive a message after subscribing?
Remember you must connect before subscribing and start the loop to process messages.
First connect to broker, then subscribe to topic, then start the loop to process network events, then messages trigger the callback.
Given this code snippet, why does the client never receive messages?
import paho.mqtt.client as mqtt
def on_message(client, userdata, msg):
print(f"Received: {msg.payload.decode()}")
client = mqtt.Client()
client.on_message = on_message
client.connect('broker.hivemq.com', 1883, 60)
client.subscribe('test/topic')
# Missing client.loop_start() or loop_forever()Think about how paho-mqtt processes incoming messages.
Without calling loop_start() or loop_forever(), the client does not process incoming messages, so the callback never triggers.
Which code snippet correctly configures a paho-mqtt client to securely connect to a broker using TLS with certificate verification?
import paho.mqtt.client as mqtt client = mqtt.Client() # Configure TLS here client.connect('mqtt.example.com', 8883, 60) client.loop_start()
Look for the method that sets CA and client certificates for TLS.
Using tls_set() with CA cert, client cert, and key enables secure TLS with verification. Option B lacks client cert and key, C disables verification, D is incomplete.