import socket
from threading import Thread
import json
import time
from encodings.punycode import Codec


class xpr_conn(Thread):
    id = ""
    timeoutInstalled = False

    def __init__(self, sock):
        Thread.__init__(self)
        self.conn = sock
        self.start()

    def __del__(self):
        self.conn.close()
        print("closed")

    def run(self):
        print("conn start")
        # получаем id
        data = self.conn.recv(1024)
        if not data: return
        self.id = data.decode("utf-8")
        if self.id != "xcontrol": return

        while True:
            dic = {}
            if not self.timeoutInstalled:
                dic["__ss__"] = {"timeout_tcp": 10}
                self.timeoutInstalled = True
            else: time.sleep(5)

            self.conn.send(("@" + json.dumps(dic)).encode("utf-8"))

            # может быть таймаут, тогда будет ошибка
            try: data = self.conn.recv(1024)
            except: data = 0

            if not data: return
            data = data.decode("utf-8")
            # если не json, выходим
            try: dic = json.loads(data)
            except: return
            else: print(dic)


Created: 12/12/2017
Visits: 89
Online: 0