0

I'd like to execute the following code using a multithreaded solution

Can anyone suggest how to improve the solution?

from selenium import webdriver

with open('proxy.txt', 'r') as f: 
    for line in f:
        print ("Connected with IP: {}".format(line))
        PROXY = line # IP:PORT or HOST:PORT
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--proxy-server=http://%s' % PROXY)
        driver = webdriver.Chrome(chrome_options=chrome_options)
        driver.get("http://whatismyipaddress.com")
        driver.quit()
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
mark contira
  • 23
  • 1
  • 2

1 Answers1

3
import os 
from multiprocessing import Pool

from selenium import webdriver

def check_ip(proxy):
    print ("Connected with IP: {}".format(proxy))
    options = webdriver.chrome.options.Options()
    options.add_argument('--proxy-server=http://{}'.format(proxy))
    driver = webdriver.Chrome(options=options)
    driver.get("http://whatismyipaddress.com")
    driver.quit()

if __name__ == '__main__':
    with open('./proxy.txt') as f:
        proxies = f.read().splitlines()
    with Pool() as pool:
        pool.map(check_ip, proxies)

Solution is based on the answer in this question

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65