Python MQTT Experiment

Source: Internet
Author: User

Both client:

  • pub_1.py
    ImportPaho.mqtt.client as MqttImport TimeImportSYSImportRandomdefon_connect (client, UserData, flags, RC):Print("connected with result code"+str (RC))defon_publish (client, UserData, mid):Print("Publish au/csie/5/9/5912/temperture",) client=Mqtt. Client () Client.on_connect=On_connectclient.on_publish=On_publishclient.connect ("iot.eclipse.org", 1883, 60) whileTrue:Try: With open ("C:\\users\\86390\\desktop\\test_s.txt","R") as F:data=F.readlines ()#data = Random.randint (.)        #client.publish ("Au/csie/5/9/5912/tempeture", str (data) + "C")        Print(len (data)) Time.sleep (2)    exceptKeyboardinterrupt:Print("EXIT") Client.disconnect () sys.exit (0)
  • sub_1.py
    ImportPaho.mqtt.client as MqttImport TimeImportSYSImportRandomdefon_connect (client, UserData, flags, RC):Print("Connected with result code"+str (RC)) Client.subscribe ("/inode/mychannel")defon_message (client, UserData, msg):Print(Len (str (msg.payload))/1024/1024) Client=Mqtt. Client () Client.on_connect=On_connectclient.on_message=On_messageclient.connect ("127.0.0.1", 1883, 60) Client.loop_forever ()

Publisher and Client:

  • pub_2.py
    ImportPaho.mqtt.publish as PublishImport TimedefTransmitmqtt (STRMSG): Strmqttbroker="localhost"    #strbroker = "127.0.0.1"Strmqttchannel ="/inode/mychannel"    Print(Len (str (STRMSG))) Publish.single (Strmqttchannel, STRMSG, hostname=strmqttbroker)if __name__=='__main__':     whileTrue:transmitmqtt ("a"*1024*1024*40) Time.sleep (0.1)        Print(Time.time ())

  • sub_2.py
    ImportPaho.mqtt.client as MqttImport TimeImportSYSImportRandomdefon_connect (client, UserData, flags, RC):Print("Connected with result code"+str (RC)) Client.subscribe ("/inode/mychannel")defon_message (client, UserData, msg):Print(Len (str (msg.payload))/1024/1024) Client=Mqtt. Client () Client.on_connect=On_connectclient.on_message=On_messageclient.connect ("127.0.0.1", 1883, 60) Client.loop_forever ()

Keng:

    • The two clients that normally communicate on Windows, which are not received on the sub side of the virtual machine, could be a DNS network management problem.
    • Two client communication, the intermediary broker must be built, but if it is a pub, a sub, it is directly through the local host as the broker.

Python MQTT Experiment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.