Use the Python library paho.mqtt.client to impersonate the MQTT Client Connection Broker,publish topic.
#-*-coding:utf-8-*-ImportPaho.mqtt.client as MqttclassMqtthandle (object):def __init__(self,mqtt_info): Self.mqtt_info=Mqtt_infodefon_connect (client, UserData, flags, RC):Print("Connected with result code"+str (RC)) Client.subscribe ("Chat") defon_message (client, UserData, msg):Print("topic:"+ Msg.topic +"Payload:"+str (msg.payload))defPublish (self): client=Mqtt. Client () Client.on_connect=Mqtthandle.on_connect client.on_message=mqtthandle.on_message Client.username_pw_set (self.mqtt_info['username'], self.mqtt_info['Password']) client.connect (self.mqtt_info['Host'], self.mqtt_info['Port'], 60) client.publish (self.mqtt_info['Topic'], str (self.mqtt_info['Payload'])) #Client.loop_forever ()Client.disconnect ()Print('Publish topic over')if __name__=="__main__": Mqtt_info={ 'username':'username', 'Password':'Password', 'Host':'10.10.10.10', 'Port': 1833, 'Topic':'Test', 'Payload':'Hello World',} MQTTC=Mqtthandle (Mqtt_info) mqttc.publish ()
Python MQTT Client publish operation