Irod

Untitled

Dec 4th, 2020
394
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import paho.mqtt.client as mqtt
  2.  
  3.  
  4. # The callback for when the client receives a CONNACK response from the server.
  5. def on_connect(client, userdata, flags, rc):
  6.     print("Connected with result code " + str(rc))
  7.  
  8.     # Subscribing in on_connect() means that if we lose the connection and
  9.     # reconnect then subscriptions will be renewed.
  10.     client.subscribe("sprc/chat/#")
  11.  
  12.  
  13. # The callback for when a PUBLISH message is received from the server.
  14. def on_message(client, userdata, msg):
  15.     print(msg.topic + " " + str(msg.payload))
  16.  
  17.  
  18. client = mqtt.Client()
  19. client.on_connect = on_connect
  20. client.on_message = on_message
  21.  
  22. client.connect("broker.hivemq.com", 1883, 60)
  23.  
  24. # Blocking call that processes network traffic, dispatches callbacks and
  25. # handles reconnecting.
  26. # Other loop*() functions are available that give a threaded interface and a
  27. # manual interface.
  28. client.loop_start()
  29. import sys
  30.  
  31. while True:
  32.     for line in sys.stdin:
  33.         client.publish('sprc/chat/Dorinel', line.strip(), qos=2)
  34.  
RAW Paste Data