Tanas

handler

Sep 30th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. import paho.mqtt.publish as publish
  2. import tkinter.scrolledtext as tkst
  3. import paho.mqtt.subscribe as subscribe
  4. import paho.mqtt.client as mqtt
  5. import datetime
  6.  
  7. class Mqtt_handler(object):
  8.     def __init__(self,client_id,password,username):
  9.         self.client = None
  10.         self.client_id = client_id
  11.         self.password = password
  12.         self.username = username
  13.  
  14.     def log_me_in(self):
  15.         print("I will try to login omg niggers")
  16.         print(self.client_id, self.password, self.username)
  17.        
  18.         self.client = mqtt.Client(client_id=self.client_id)
  19.         self.client.on_connect = self.on_connect
  20.         self.client.on_message = self.on_message
  21.         try:
  22.             self.client.username_pw_set(self.username, password=self.password)
  23.             self.client.connect("pcfeib425t", 1883, 60)
  24.             self.client.loop_start()
  25.         except TimeoutError:
  26.             print("Connection timeout. Remote MQTT broker is currently unavailable. Check whether the broker is running.")
  27.             exit(0)
  28.    
  29.     @staticmethod
  30.     def on_connect(client, userdata, flags, rc):
  31.         print("Connected with result code "+str(rc))
  32.  
  33.         # Subscribing in on_connect() means that if we lose the connection and
  34.         # reconnect then subscriptions will be renewed.
  35.         client.subscribe("/mschat/all/#")
  36.  
  37.     # The callback for when a PUBLISH message is received from the server.
  38.     @staticmethod
  39.     def on_message(self, userdata, msg):
  40.         #app.printOutMessages(msg.topic+" "+str(msg.payload))
  41.         print(msg.topic+" "+str(msg.payload))
Add Comment
Please, Sign In to add comment