有时候需要测试某些IP是否提供了HTTP代理功能。于是写了如下代码,对192.168.x.x这个IP段进行扫描测试
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
|
# -*- coding:utf-8 -*-
import
socket
import
threading
import
Queue
timeout
=
2
socket.setdefaulttimeout(timeout)
threadnum
=
256
ProxyPorts
=
[
80
,
1080
,
3128
,
8080
,
8081
]
reqcontent
=
"GET / HTTP/1.1\r\n"
+
\
"Host: ip.cn\r\n"
+
\
"Accept: */*\r\n"
+
\
"User-Agent: curl/7.35.0curl/7.35.0\r\n\r\n"
def
about():
print
"####################################################"
print
"find proxy"
print
"####################################################"
class
ipQueueCrawler(threading.Thread):
def
__init__(
self
, ipQueue):
threading.Thread.__init__(
self
)
self
.ipQueue
=
ipQueue
def
run(
self
):
#Overwrite run() method, put what you want the thread do here
while
not
self
.ipQueue.empty():
CurIp
=
self
.ipQueue.get()
print
"left:"
+
str
(
self
.ipQueue.qsize())
+
'\r'
,
for
ProxyPort
in
ProxyPorts:
try
:
s
=
socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((CurIp,ProxyPort))
s.sendall(reqcontent)
data
=
s.recv(
500
)
if
"IP:"
+
CurIp
in
data:
print
CurIp
+
":"
+
str
(ProxyPort)
else
:
pass
#print self.proxyIP + ":" + str(self.proxyPort) + ":No"
s.close()
except
:
pass
#print self.proxyIP + ":" + str(self.proxyPort) + ":Except"
if
__name__
=
=
'__main__'
:
about()
ipQueue
=
Queue.Queue(maxsize
=
256
*
256
+
1
)
ipPrefix
=
"192.168."
for
ipMid
in
range
(
0
,
256
):
for
ipLast
in
range
(
1
,
256
):
ipQueue.put(ipPrefix
+
str
(ipMid)
+
"."
+
str
(ipLast))
for
i
in
range
(
0
,threadnum):
ipQueueCrawler(ipQueue).start()
|