Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Simple operation of emqtt by python3

2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

You need to download the external package first

Pip install paho-mqtt publisher import paho.mqtt.client as mqttHOST = "192.168.44.31" PORT = 1883def Publish_Message (): client = mqtt.Client () client.username_pw_set (username='admin', password='public') # for client.connect (HOST, PORT, 60) for x in range (10): Message = "hello" + str (x) client.publish ("services", Message) 0) # publish a message with the theme 'services', content is' hello x'# time.sleep (10) client.loop_forever () # keep connected if _ _ name__ = ='_ main__': Publish_Message ()

Publisher, main content: publish ("topic_name", "utf8_message", Qos)

Qos:0-- > the sender only sends a message once without retrying, and Broker will not return a confirmation message. Broker may not have received the message. (default) Qos:1-- > the sender sends a message at least once to ensure that the message reaches Broker,Broker and a confirmation message PUBACK is returned. Broker may receive a duplicate message. Qos:2-- > two-phase acknowledgements are used to ensure that messages are not lost and duplicated. Broker will definitely receive the message, and only once. Subscriber import paho.mqtt.client as mqttdef on_connect (client, userdata, flags, rc): print ("Connected with result code" + str (rc)) # rc indicates the returned status value client.subscribe ("services") def on_message (client, userdata) Msg): print (msg.topic+ "+": "+ str (msg.payload)) if _ _ name__ = =" _ _ main__ ": client = mqtt.Client () client.on_connect = on_connect client.on_message = on_message client.username_pw_set (username='admin', password='public') # for client.connect (" 192.168.44.31 ", 1883) after adding the plug-in authentication method 60) client.loop_forever ()

Subscriber, the output of information is in msg.payload

What the rc value represents:

0: Connection successful

1: Connection refused-incorrect protocol version

2: Connection refused-invalid client identifier

3: Connection refused-server unavailable

4: Connection refused-bad username or password

5: Connection refused-not authorised

6-255: Currently unused.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report