forked from maxmalysh/python-proxy-checker
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.py
More file actions
Latest commit
181 lines (139 loc) · 4.01 KB
/
Copy pathproxy.py
File metadata and controls
181 lines (139 loc) · 4.01 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Network
importurllib.request
importurllib.parse
importurllib.error
importhttp.cookiejar
# Concurrency
importthreading
importqueue
importitertools
# Etc
importtime
# Global variables
# in_filename = 'input/3.txt'
filetocheck='proxys.txt'
out_filename='checkedproxylist.txt'
proxy_type=input("Enter proxy type: ")
# use "google.cn" to include Chinese servers
test_url='http://www.google.cn/humans.txt'
thread_number=200
timeout_value=10
ok_msg="OK! "
fail_msg="FAIL "
# Stats
good_proxy_num=itertools.count()
# Safe print()
mylock=threading.Lock()
defsprint(*a, **b):
withmylock:
print(*a, **b)
#
# Printer
#
classPrintThread(threading.Thread):
def__init__(self, queue, filename):
threading.Thread.__init__(self)
self.queue=queue
self.output=open(filename, 'a')
self.shutdown=False
defwrite(self, line):
print(line, file=self.output)
defrun(self):
whilenotself.shutdown:
lines=self.queue.get()
self.write(lines)
self.queue.task_done()
defterminate(self):
self.output.close()
self.shutdown=True
#
# Processor
#
classProcessThread(threading.Thread):
def__init__(self, id, task_queue, out_queue):
threading.Thread.__init__(self)
self.task_queue=task_queue
self.out_queue=out_queue
self.id=id
# ...
defrun(self):
whileTrue:
task=self.task_queue.get()
result=self.process(task)
ifresultisnotNone:
self.out_queue.put(result)
next(good_proxy_num)
self.task_queue.task_done()
# Do the processing job here
defprocess(self, task):
proxy=task
log_msg=str("Thread #%3d. Trying proxy %21s \t\t"% (self.id, proxy))
cj=http.cookiejar.CookieJar()
opener=urllib.request.build_opener(
urllib.request.HTTPCookieProcessor(cj),
urllib.request.HTTPRedirectHandler(),
urllib.request.ProxyHandler({proxy_type: proxy})
)
try:
t1=time.time()
response=opener.open(test_url, timeout=timeout_value).read()
t2=time.time()
exceptExceptionase:
log_msg+="%s (%s)"% (fail_msg, str(e))
sprint(log_msg)
returnNone
log_msg+=ok_msg+" Response time: %d, length=%s"% (int((t2-t1) *1000), str(len(response)))
sprint(log_msg)
returnproxy
defterminate(self):
None
# print("Thread #%d is down..." % (self.id))
#
# Main starts here
#
# Init some stuff
input_queue=queue.Queue()
result_queue=queue.Queue()
# Spawn worker threads
workers= []
foriinrange(0, thread_number):
t=ProcessThread(i, input_queue, result_queue)
t.setDaemon(True)
t.start()
workers.append(t)
# Spawn printer thread to print
f_printer=PrintThread(result_queue, out_filename)
f_printer.setDaemon(True)
f_printer.start()
# Add some stuff to the input queue
start_time=time.time()
withopen(filetocheck) asproxyfile:
proxy_list=proxyfile.read().split("\n")
proxyfile.close()
forproxyinproxy_list:
input_queue.put(proxy)
total_proxy_num=len(proxy_list)
print("got %d proxies to check"%total_proxy_num)
iftotal_proxy_num==0:
exit()
# Wait for queue to get empty
input_queue.join()
result_queue.join()
# while (not input_queue.empty()):
# time.sleep(1)
# Shutdown
f_printer.terminate()
forworkerinworkers:
worker.terminate()
# Print some info
good_proxy_num=float(next(good_proxy_num))
print("In: %d. Good: %d, that's %.2f%%"% (total_proxy_num, good_proxy_num, 100.0*good_proxy_num/total_proxy_num))
end_time=time.time()
print("Time elapsed: %.1f seconds."% (end_time-start_time))
print("Bye-bye!")
#############
# Read file, convert it to list of proxies.
# Add proxies to queue
# Launch N (10) threads
# When writing to the file, use lock
# When Queue is empty flash results and shutdown