I’m writing a script to access a website using proxies with multiple threads but now I’m stuck in multiple threads, when I run the script below, it opens 5 browsers but all 5 use 1 proxy, I want 5 browsers to use different proxies, can someone help me complete it? thank you
Here is my script :
JavaScript
x
37
37
1
from selenium import webdriver
2
from selenium import webdriver
3
import time , random
4
import threading
5
6
7
def e():
8
9
a = open("sock2.txt", "r")
10
for line in a.readlines():
11
12
b = line
13
prox = b.split(":")
14
IP = prox[0]
15
PORT = int(prox[1].strip("n"))
16
print(IP)
17
print(PORT)
18
19
20
profile = webdriver.FirefoxProfile()
21
profile.set_preference("network.proxy.type", 1)
22
profile.set_preference("network.proxy.socks", IP)
23
profile.set_preference("network.proxy.socks_port", PORT)
24
try:
25
26
driver = webdriver.Firefox(firefox_profile=profile)
27
driver.get("http://www.whatsmyip.org/")
28
except:
29
print("Proxy Connection Error")
30
driver.quit()
31
else:
32
time.sleep(random.randint(40, 70))
33
driver.quit()
34
for i in range(5):
35
t = threading.Thread(target=e)
36
t.start()
37
(Wish everyone has a happy and lucky new year)
Advertisement
Answer
Dominik Lašo captured it correctly – each threads processes the file from the beginning. Here’s probably how it should look like:
JavaScript
1
38
38
1
from selenium import webdriver
2
from selenium import webdriver
3
import time , random
4
import threading
5
6
7
def e(ip, port):
8
profile = webdriver.FirefoxProfile()
9
profile.set_preference("network.proxy.type", 1)
10
profile.set_preference("network.proxy.socks", IP)
11
profile.set_preference("network.proxy.socks_port", PORT)
12
try:
13
driver = webdriver.Firefox(firefox_profile=profile)
14
driver.get("http://www.whatsmyip.org/")
15
except:
16
print("Proxy Connection Error")
17
driver.quit()
18
else:
19
time.sleep(random.randint(40, 70))
20
driver.quit()
21
22
my_threads = []
23
with open("sock2.txt", "r") as fd:
24
for line in fd.readlines():
25
line = line.strip()
26
if not line:
27
continue
28
prox = line.split(":")
29
ip = prox[0]
30
port = int(prox[1])
31
print('-> {}:{}'.format(ip, port))
32
t = threading.Thread(target=e, args=(ip, port,))
33
t.start()
34
my_threads.append(t)
35
36
for t in my_threads:
37
t.join()
38