def setUp(self): self.output = OutPut() KB["console_width"] = getTerminalSize() KB["task_queue"] = Queue() KB["finished"] = 100 KB["start_time"] = time.time() print(KB)
def run_threads(num_threads, thread_function, args: tuple = ()): threads = [] KB["continue"] = True KB["console_width"] = getTerminalSize() KB['start_time'] = time.time() KB['finished'] = 0 KB["lock"] = threading.Lock() KB["result"] = 0 KB["running"] = 0 try: info_msg = "Staring {0} threads".format(num_threads) logger.info(info_msg) # Start the threads for num_threads in range(num_threads): thread = threading.Thread(target=exception_handled_function, name=str(num_threads), args=(thread_function, args)) thread.setDaemon(True) try: thread.start() except Exception as ex: err_msg = "error occurred while starting new thread ('{0}')".format( str(ex)) logger.critical(err_msg) break threads.append(thread) # And wait for them to all finish alive = True while alive: alive = False for thread in threads: if thread.isAlive(): alive = True time.sleep(0.1) except KeyboardInterrupt as ex: KB['continue'] = False if num_threads > 1: logger.info("waiting for threads to finish{0}".format( " (Ctrl+C was pressed)" if isinstance(ex, KeyboardInterrupt ) else "")) try: while threading.activeCount() > 1: pass except KeyboardInterrupt: raise except Exception as ex: logger.error("thread {0}: {1}".format( threading.currentThread().getName(), str(ex))) traceback.print_exc() finally: Share.dataToStdout('\n')
def _init_kb(): KB['continue'] = True KB['registered'] = dict() KB['task_queue'] = Queue() KB["is_win"] = platform.system() == 'Windows' KB["spiderset"] = SpiderSet() KB["console_width"] = getTerminalSize() KB['start_time'] = time.time() KB['finished'] = 0 KB["lock"] = threading.Lock() KB["result"] = 0 KB["running"] = 0
def initKb(): KB['continue'] = False # 线程一直继续 KB['registered'] = dict() # 注册的漏洞插件列表 KB['fingerprint'] = dict() # 注册的指纹插件列表 KB['task_queue'] = Queue() # 初始化队列 KB["spiderset"] = SpiderSet() # 去重复爬虫 KB["console_width"] = getTerminalSize() # 控制台宽度 KB['start_time'] = time.time() # 开始时间 KB["lock"] = threading.Lock() # 线程锁 KB["output"] = OutPut() KB["running_plugins"] = dict() KB['finished'] = 0 # 完成数量 KB["result"] = 0 # 结果数量 KB["running"] = 0 # 正在运行数量