-
Notifications
You must be signed in to change notification settings - Fork 3
/
proxy.py
75 lines (63 loc) · 1.67 KB
/
proxy.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
import urllib2, socket
import threading
socket.setdefaulttimeout(8)
proxy = 'proxy.txt'
threads = []
proxys = []
threadLock = threading.Lock()
class myThread (threading.Thread):
def __init__(self, threadID, name, pip):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.pip = pip
def run(self):
global proxys
#print "Starting " + self.name
if is_bad_proxy(self.pip):
print "Bad Proxy:", self.pip
else:
print self.pip, " is working"
add_proxy(self.pip)
#print "Exiting " + self.name
def add_proxy(pip):
global proxys
proxys.append(pip)
def is_bad_proxy(pip):
try:
proxy_handler = urllib2.ProxyHandler({'http': pip})
opener = urllib2.build_opener(proxy_handler)
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib2.install_opener(opener)
req=urllib2.Request('http://www.google.com') # change the url address here
sock=urllib2.urlopen(req)
except urllib2.HTTPError, e:
print 'Error code: ', e.code
return e.code
except Exception, detail:
print "ERROR:", detail
return 1
return 0
def checkProxy():
global proxy,threads,proxys
proxyList = []
with open(proxy, 'r') as file:
for line in file:
proxyList.append(line.rstrip())
while len(proxyList)>0:
for num in range (0,30):
threads.append(myThread(num,"Thread-"+str(num), proxyList[0]))
proxyList.pop(0)
if(len(proxyList)<=0):
break
for t in threads:
t.start()
for t in threads:
t.join()
threads = []
proxys = list(set(proxys))
print 'Total proxy : ' + str(len(proxys))
f = open(proxy,'w')
for p in proxys:
f.write(p+'\n')
f.close()