go back to single-threaded (because of chromedriver)

+ delete tor data directory when closing
master
emamaker 2022-01-31 22:26:20 +01:00
parent 642fe380de
commit 56530c96c7
2 changed files with 18 additions and 18 deletions

View File

@ -8,6 +8,8 @@ import undetected_chromedriver as uc
import threading import threading
data_directory = ""
def find_free_port(): def find_free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('', 0)) s.bind(('', 0))
@ -17,13 +19,15 @@ def find_free_port():
def create_tor_proxy(socks_port,control_port): def create_tor_proxy(socks_port,control_port):
TOR_PATH = os.environ['TOR_PATH'] TOR_PATH = os.environ['TOR_PATH']
data_directory = "tor-data-dir-" + (str(threading.get_native_id()))
try: try:
tor_process = stem.process.launch_tor_with_config( tor_process = stem.process.launch_tor_with_config(
config = { config = {
'SocksPort': str(socks_port), 'SocksPort': str(socks_port),
'ControlPort' : str(control_port), 'ControlPort' : str(control_port),
'MaxCircuitDirtiness' : '300', '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, init_msg_handler = lambda line: print(line) if re.search('Bootstrapped', line) else False,
tor_cmd = TOR_PATH tor_cmd = TOR_PATH
@ -36,9 +40,10 @@ def create_tor_proxy(socks_port,control_port):
return tor_process return tor_process
def start_browser(use_tor=False): def start_browser(use_tor=False, headless=False):
options = uc.ChromeOptions() options = uc.ChromeOptions()
options.add_argument('--no-first-run') options.add_argument('--no-first-run')
options.add_argument('--password-store=basic') options.add_argument('--password-store=basic')
@ -62,3 +67,10 @@ def start_browser(use_tor=False):
return driver, tor_process return driver, tor_process
def close_browser(driver, tor_process):
if tor_process:
os.remove(data_directory)
tor_process.kill()
driver.quit()

View File

@ -107,24 +107,12 @@ def click(driver, by, desc, timeout):
WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((by, desc))).click() WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((by, desc))).click()
def launch_and_visit(use_tor, page_url): 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) visit_page(driver, page_url)
time.sleep(5) time.sleep(5)
driver.quit() browser_manager.close_browser(driver, tor_process)
if use_tor:
tor_process.kill()
if __name__ == "__main__": if __name__ == "__main__":
threads = [] launch_and_visit(use_tor=True, page_url='https://giangillorossi.altervista.org')
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()