Ejemplo n.º 1
0
 def handle_data(self, data):
     try:
         obj = json.loads(data)
         STD.flush(obj)
         self.task.append(obj)
     except:
         Util.err(data)
Ejemplo n.º 2
0
    def run(self):
        flag = False
        while True:
            if len(self.task_q) > 0:
                task = self.task_q.pop()
                STD.flush(task)
                if 'file_dir' in task:
                    flag = True
                    task['file_dir'] = urllib.parse.unquote(task['file_dir'])
                if 'file_name' in task:
                    task['file_name'] = urllib.parse.unquote(task['file_name'])

                if 'type' in task:
                    if task['type'] == 'php':
                        WatchPhp(task, self.file)
                    elif task['type'] == 'js':
                        WatchJs(task, self.file)
                    elif task['type'] == 'build':
                        Build(task, self.redis)
                    else:
                        NormalPack(task).process()
            else:
                if len(self.task_q) <= 0 and flag is True:
                    flag = False
                    STD.flush('fresh')
                    try:
                        if self.ws['ws']:
                            self.ws['ws'].send(self.name)
                    except:
                        Util.err()
            time.sleep(0.5)
Ejemplo n.º 3
0
    def run(self):
        while True:
            if not self.isHandleShake:
                # 开始握手阶段
                header = self.analyzeReq()
                secKey = header['Sec-WebSocket-Key']

                acceptKey = self.generateAcceptKey(secKey)

                response = "HTTP/1.1 101 Switching Protocols\r\n"
                response += "Upgrade: websocket\r\n"
                response += "Connection: Upgrade\r\n"
                response += "Sec-WebSocket-Accept: %s\r\n\r\n" % (
                    acceptKey.decode('utf-8'))
                self.con.send(response.encode())
                self.isHandleShake = True
                STD.flush('response:\r\n' + response)
                # 握手阶段结束
            else:
                # 接受客户端数据
                opcode = self.getOpcode()
                if opcode == 8:
                    self.con.close()
                self.getDataLength()
                clientData = self.readClientData()
                # STD.flush('客户端数据:' + clientData)
                self.handle_data(clientData)
                # 向客户端发送数据
                # self.sendDataToClient('hello world')
            time.sleep(0.1)
Ejemplo n.º 4
0
 def process(self):
     f = OS.open(self.watch_file, 'w')
     f.write(''.join(self.lines))
     f.close()
     if self.host != '127.0.0.1':
         STD.flush('start upload')
         self.upload()
Ejemplo n.º 5
0
 def __init__(self, task_q, file, ws, wsName):
     super().__init__()
     self.task_q = task_q
     self.file = file
     self.name = wsName
     STD.flush('debug: ' + wsName)
     self.ws = ws
     pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
     self.redis = redis.StrictRedis(connection_pool=pool)
Ejemplo n.º 6
0
 def run(self):
     redis_server = os.path.abspath(
         '../vendor/redis_2.4.5_64/redis-server.exe')
     child = subprocess.Popen(redis_server,
                              shell=True,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.STDOUT)
     STD.flush('redis run...')
     while True:
         time.sleep(10)
         # line = child.stdout.readline()
         if subprocess.Popen.poll(child) == 0:  # 判断子进程是否结束
             break
Ejemplo n.º 7
0
 def run(self):
     (ConnectWs(self.ws)).start()
     (ReadTask(self.task, self.file, self.ws, TIME.now())).start()
     self.sock.bind(('127.0.0.1', self.port))
     self.sock.listen(5)
     STD.flush('WebSocket server run...')
     while True:
         try:
             connection, address = self.sock.accept()
             returnCrossDomain(connection, self.task, self.file).start()
         except:
             Util.err()
         finally:
             time.sleep(0.1)
Ejemplo n.º 8
0
 def run(self):
     if sys.argv[3]:
         php = os.path.abspath(
             '../vendor/php-7.2.0-nts-Win32-VC15-x64/php.exe')
         child = subprocess.Popen(php + '-S 127.0.0.1:' + sys.argv[3],
                                  shell=True,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.STDOUT)
         os.chdir(path)
         STD.flush('httpServer run...')
         while True:
             time.sleep(10)
             # line = child.stdout.readline()
             if subprocess.Popen.poll(child) == 0:  # 判断子进程是否结束
                 break
Ejemplo n.º 9
0
    def sendDataToClient(self, text):
        sendData = struct.pack('!B', 0x81)

        length = len(text)
        if length <= 125:
            sendData += struct.pack('!B', length)
        elif length <= 65536:
            sendData += struct.pack('!B', 126)
            sendData += struct.pack('!H', length)
        elif length == 127:
            sendData += struct.pack('!B', 127)
            sendData += struct.pack('!Q', length)

        sendData += struct.pack('!%ds' % length, text.encode())
        dataSize = self.con.send(sendData)
        STD.flush('sendDataToClient:' + str(dataSize))
Ejemplo n.º 10
0
 def upload(self):
     sf = paramiko.Transport(self.host, self.port)
     sf.connect(username=self.username, password=self.password)
     sftp = paramiko.SFTPClient.from_transport(sf)
     try:
         if os.path.isdir(self.watch_file):  # 判断本地参数是目录还是文件
             for f in os.listdir(self.watch_file):  # 遍历本地目录
                 sftp.put(os.path.join(self.watch_file + f),
                          os.path.join(self.watch_file + f))  # 上传目录中的文件
         else:
             remote = self.upload_root + self.watch_file.split(self.root)[1]
             here = self.watch_file
             STD.flush(here)
             STD.flush(remote)
             sftp.put(here, remote)  # 上传文件
     except:
         Util.err()
     sf.close()
Ejemplo n.º 11
0
 def err(e=None):
     if e:
         print(e)
     STD.flush(traceback.format_exc())