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
data_directory = ""
def find_free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('', 0))
@ -17,13 +19,15 @@ 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(
config = {
'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')
@ -62,3 +67,10 @@ def start_browser(use_tor=False):
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()
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()
launch_and_visit(use_tor=True, page_url='https://giangillorossi.altervista.org')