-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathproxy-rotation.py
77 lines (60 loc) · 1.84 KB
/
proxy-rotation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from fake_useragent import UserAgent
import requests
from bs4 import BeautifulSoup
import random
import re
from threading import Thread
ua = UserAgent()
# Rotating User-Agent
def rotate_agent():
return ua.random
def random_proxy(proxies):
return random.randint(0, len(proxies)-1)
PATH = 'out.txt'
# Collecting proxies
def getProxies():
Req = requests.get('https://www.sslproxies.org/',
headers={'User-Agent': rotate_agent()})
bs = BeautifulSoup(Req.content, features="html.parser")
Proxies = []
for row in bs.tbody.find_all('tr'):
cols = row.find_all('td')
Proxies.append({'IP': cols[0].text, 'Port': cols[1].text})
return Proxies
def addDataToFile(ip):
proxies = []
with open(PATH, 'r') as f:
proxies = f.readlines()
if ip not in proxies or len(proxies) == 0:
with open(PATH, 'a') as f:
f.write(ip)
def check(ip, port):
try:
Req = requests.get('http://icanhazip.com/', headers={
'User-Agent': rotate_agent()}, proxies={'https': ip + ':'+port}, timeout=10)
#
# Scrape,..,..,DO something
#
myip = re.sub(r'[^0-9^\.:]', '', str(Req.content))
print('#Rotated --> {}:{}'.format(ip, port))
addDataToFile('{}:{}\n'.format(ip, port))
except:
print('#Dead --> {}:{}'.format(ip, port))
# Rotating proxies each request
Proxies = getProxies()
NUMBER_OF_THREADS = 10
i = 0
while True:
Threads = []
for k in range(i, i+NUMBER_OF_THREADS):
i += 1
if(k >= len(Proxies)):
break
th = Thread(target=check, args=[Proxies[k]['IP'], Proxies[k]['Port']])
th.start()
Threads.append(th)
for th in Threads:
th.join()
#os.system('cls' if os.name == 'nt' else 'clear')
if(i >= len(Proxies)):
break