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 run(self): try: # protocols=['chat'] self.ws['ws'].run_forever() except: Util.err() self.ws['ws'].close()
def handle_data(self, data): try: obj = json.loads(data) STD.flush(obj) self.task.append(obj) except: Util.err(data)
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, **kwargs): self.debug = Util.get_key('debug', kwargs, Util.is_test()) self._201 = {} self._204 = {} self.config = { 'txz_audio_qqmusic': 204, 'TXZ_AUDIO_DATA': 201, 'txz_audio_leTing': 204, 'TXZ_AUDIO': 201, 'txz_audio_sort': 204, 'txz_audio_himalaya': 204, 'txz_audio_kaola': 204, 'txz_audio_history': 204 } self.connections = {} self.initDbConfig()
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 make_set(set_dict): set_str = '' for key in set_dict: val = set_dict[key] if TYPE.is_str(val): val = "'" + Util.addslashes(val) + "'," elif TYPE.is_int(val): val = str(val) + ',' set_str += "`" + key + "`=" + val return set_str[:-1]
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 __init__(self, pack): try: # 打包babel f_new_path = pack.watch_file + '_new' print(f_new_path) f_new_fp = OS.open(f_new_path, 'w') f_new_fp.write(''.join(pack.lines)) f_new_fp.close() f_babel_path = pack.watch_file + '_babel' cmd = 'babel %s -o %s' % (f_new_path, f_babel_path) child = subprocess.Popen(cmd, shell=True) child.wait() f_babel_fp = OS.open(f_babel_path) pack.lines = f_babel_fp.readlines(9999999) f_babel_fp.close() os.remove(f_new_path) os.remove(f_babel_path) except: Util.err()
def make_where_item(item): if not TYPE.is_list(item): item = [item, '=', 'and'] else: length = len(item) if length < 2: item.append('=') item.append('and') elif length < 3: item.append('and') if TYPE.is_str(item[0]): item[0] = "'" + Util.addslashes(item[0]) + "' " + item[2] + " " elif TYPE.is_int(item[0]): item[0] = str(item[0]) + " " + item[2] + " " return item
def __init__(self, **kwargs): self.max_thread_num = Util.get_key('max_thread_num', kwargs, 1) self.no_use_thread = [i for i in range(self.max_thread_num)]
def str_to_time(string): try: return int(mktime(strptime(str(string), '%Y-%m-%d %H:%M:%S'))) except: Util.err() return 0