# MQTT Python client library
Eclipse Paho Python (opens new window) is a Python language client library under the Eclipse Paho project, which can connect to MQTT Broker to publish messages, subscribe to topics and receive Published message.
Install using the PyPi package management tool:
pip install paho-mqtt
Copied!
1
# Paho Python usage example
This example contains the complete code of Paho Python in Python connecting to EMQX Broker, sending and receiving messages:
import paho.mqtt.client as mqtt #Connection success callback def on_connect(client, userdata, flags, rc): print('Connected with result code '+str(rc)) client.subscribe('testtopic/#') # Message receiving callback def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) client = mqtt.Client() # Specify callback function client.on_connect = on_connect client.on_message = on_message # Establish a connection client.connect('broker.emqx.io', 1883, 60) # Publish a message client.publish('emqtt',payload='Hello World',qos=0) client.loop_forever()
Copied!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Paho Python MQTT 5.0 support
Currently, Paho Python is still adapting to MQTT 5.0 and has not yet been fully supported it.
What’s on this page