From 56530c96c729e90d166a2d6dcf074dd890c3d5ba Mon Sep 17 00:00:00 2001 From: emamaker Date: Mon, 31 Jan 2022 22:26:20 +0100 Subject: [PATCH] go back to single-threaded (because of chromedriver) + delete tor data directory when closing --- browser_manager.py | 18 +++++++++++++++--- page_viewer.py | 18 +++--------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/browser_manager.py b/browser_manager.py index 2bdffa8..042d259 100644 --- a/browser_manager.py +++ b/browser_manager.py @@ -8,6 +8,8 @@ import undetected_chromedriver as uc import threading +data_directory = "" + def find_free_port(): with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: s.bind(('', 0)) @@ -16,6 +18,8 @@ def find_free_port(): def create_tor_proxy(socks_port,control_port): TOR_PATH = os.environ['TOR_PATH'] + + data_directory = "tor-data-dir-" + (str(threading.get_native_id())) try: tor_process = stem.process.launch_tor_with_config( @@ -23,7 +27,7 @@ def create_tor_proxy(socks_port,control_port): 'SocksPort': str(socks_port), 'ControlPort' : str(control_port), 'MaxCircuitDirtiness' : '300', - 'DataDirectory' : "tor-data-dir-" + (str(threading.get_native_id())) + 'DataDirectory' : data_directory }, init_msg_handler = lambda line: print(line) if re.search('Bootstrapped', line) else False, tor_cmd = TOR_PATH @@ -36,9 +40,10 @@ def create_tor_proxy(socks_port,control_port): return tor_process -def start_browser(use_tor=False): +def start_browser(use_tor=False, headless=False): options = uc.ChromeOptions() + options.add_argument('--no-first-run') options.add_argument('--password-store=basic') @@ -61,4 +66,11 @@ def start_browser(use_tor=False): driver = uc.Chrome(options=options) return driver, tor_process - \ No newline at end of file + + +def close_browser(driver, tor_process): + if tor_process: + os.remove(data_directory) + tor_process.kill() + + driver.quit() diff --git a/page_viewer.py b/page_viewer.py index c7886e0..0228381 100644 --- a/page_viewer.py +++ b/page_viewer.py @@ -107,24 +107,12 @@ def click(driver, by, desc, timeout): WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((by, desc))).click() def launch_and_visit(use_tor, page_url): - driver, tor_process = browser_manager.start_browser(use_tor) + driver, tor_process = browser_manager.start_browser(use_tor=use_tor) visit_page(driver, page_url) time.sleep(5) - driver.quit() - - if use_tor: - tor_process.kill() + browser_manager.close_browser(driver, tor_process) if __name__ == "__main__": - threads = [] - - for i in range(0, 3): - t1 = threading.Thread(target=launch_and_visit, args=(True, 'https://giangillorossi.altervista.org')) - t1.start() - - threads.append(t1) - - for t in threads: - t.join() \ No newline at end of file + launch_and_visit(use_tor=True, page_url='https://giangillorossi.altervista.org') \ No newline at end of file