-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxypool.py
More file actions
70 lines (52 loc) · 1.7 KB
/
proxypool.py
File metadata and controls
70 lines (52 loc) · 1.7 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
import re
import requests
url = 'http://cn-proxy.com/'
header = {'Host': 'cn-proxy.com',
'Referer': 'https://www.google.com/',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'}
research = re.compile(r'<td>(?P<ip>.*)</td>\n<td>(?P<port>.*)</td>\n<td>(?P<iplocal>.*)</td>')
def runtime(func):
def wrapper(*args, **kwargs):
import time
start = time.time()
run = func(*args, **kwargs)
end = time.time()
timeuse = end - start
return run,timeuse
return wrapper
def getip():
r = requests.get(url,headers = header)
a = research.findall(r.text)
buf = []
for i in a[1:]:
buf.append(i)
return buf
@runtime
def checkip(ip, port):
proxies = {
"http": "http://{}:{}".format(ip, port),
"https": "http://{}:{}".format(ip, port)
}
try:
print("开始验证 {}:{}".format(ip, port))
r = requests.get('http://ip.cn', proxies=proxies, timeout=30)
check = re.findall(r'<code>(.*?)</code>', r.text)
if check:
return 1
except:
return 0
def cleanresult():
with open('./result.txt', 'w') as f:
pass
def save(ip, port, iplocal, timeuse):
with open('./result.txt', 'a+') as f:
f.write("{}:{} {} {}\n".format(ip, port, iplocal, timeuse))
if __name__ == "__main__":
cleanresult()
proxyips = getip()
for i in proxyips:
ip, port, iplocal = i
checkresult, timeuse = checkip(ip, port)
if checkresult:
print("可用代理: {}:{} {} 用时:{}".format(ip, port, iplocal, timeuse))
save(ip, port, iplocal, timeuse)