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)
def handle_data(self, data): try: obj = json.loads(data) STD.flush(obj) self.task.append(obj) except: Util.err(data)
def run(self): try: # protocols=['chat'] self.ws['ws'].run_forever() except: Util.err() self.ws['ws'].close()
def operator_obj_by_sql(self, sql): try: if self.table_cursor.execute(sql): return True else: return False except: Util.err() return False
def get_obj_by_sql(self, sql): try: self.table_cursor.execute(sql) row = self.table_cursor.fetchone() if TYPE.is_tuple(row): return row else: return False except: Util.err(sql) return False
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)
def continue_browser_get(url, count=1, ref=None): counter = 0 while 1: if counter > count: break try: if ref: headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/58.0.3029.110 Safari/537.36', 'Accept': 'text/html;q=0.9,*/*;q=0.8', 'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3', # 'Accept-Encoding': 'gzip', 'Accept-Language': 'zh-CN,zh;q=0.8', # # 'X-Forwarded-For': Util.get_random_ip(), # 'Connection': 'close', # 'Cache-Control': 'no-cache', 'Referer': ref } req = request.Request(url, headers=headers) response = request.urlopen(req) res_str = response.read().decode('utf-8', 'ignore') else: response = request.urlopen(url) res_str = response.read().decode('utf-8', 'ignore') return res_str except Exception as e: Util.err() print(url) if str(e) == 'HTTP Error 404: Not Found': counter += 1 continue if str(e) == 'Remote end closed connection without response': time.sleep(1) continue if str(e) == 'HTTP Error 502: Bad Gateway': time.sleep(1) continue if str(e) == 'HTTP Error 500: Internal Server Error': time.sleep(1) continue counter += 1 return False
def browser_get(url, req_header=None): try: req = requests.get(url, headers=req_header) if req.encoding == 'ISO-8859-1': encodings = requests.utils.get_encodings_from_content(req.text) if encodings: encoding = encodings[0] else: encoding = req.apparent_encoding else: encoding = req.apparent_encoding encode_content = req.content.decode(encoding, 'replace').encode( 'utf-8', 'replace') return encode_content.decode('utf-8', 'ignore') except: Util.err() return ''
def __init__(self, pack): for i, line in enumerate(pack.lines): pattern = 'require\([\'|\"](.*?)[\'|\"]\)' res = re.search(pattern, line) if res is None: continue tpl_path = pack.watch_file_dir + res.group(1) try: tpl_fp = OS.open(tpl_path) content = tpl_fp.read(9999999) tpl_fp.close() line2 = re.sub('require\([\'|\"](.*?)[\'|\"]\)', '`' + content + '`', line) pack.lines[i] = line2 except: Util.err() pack.lines[i] = line
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()
def process(self): try: self.require[os.path.abspath(self.watch_file)] = self.require_js for parent in self.require: if os.path.abspath(self.watch_file) in self.require[parent]: parent = parent.replace('\\dist', '') f = OS.open(parent) content = f.read(99999) if content[-1:] == '\n': content = content[:-1] else: content += '\n' f.close() f = OS.open(parent, 'w') f.write(content) f.close() except: Util.err() super().process()
def str_to_time(string): try: return int(mktime(strptime(str(string), '%Y-%m-%d %H:%M:%S'))) except: Util.err() return 0