forked from meteor1993/python-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerifyProxy.py
More file actions
53 lines (49 loc) · 1.63 KB
/
Copy pathVerifyProxy.py
File metadata and controls
53 lines (49 loc) · 1.63 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
import requests
from MysqlClient import MysqlClient
class VerifyProxy(object):
def __init__(self):
self.mysql = MysqlClient()
def verify_proxy(self, scheme, ip, port):
"""
使用百度测试代理的连通性,并返回响应时长(单位:ms)
:param scheme:
:param ip:
:param port:
:return:
"""
proxies = {
scheme: scheme + '://' + ip + ':' + port + '/'
}
response_time = 0
status = '0'
try:
response = requests.get(scheme + '://www.baidu.com/get', proxies=proxies)
if response.ok:
response_time = round(response.elapsed.total_seconds() * 1000)
status = '1'
else:
response_time = 0
status = '0'
except:
pass
return {"response_time" : response_time, "status" : status}
def verify_all(self):
"""
验证住方法,从数据库中获取所有代理进行验证
:return:
"""
results = self.mysql.find_all()
for result in results:
res = self.verify_proxy(result[1], result[2], result[3])
proxy = {
"id": result[0],
"scheme": result[1],
"ip": result[2],
"port": result[3],
"status": res["status"],
"response_time": res["response_time"],
}
self.mysql.update_proxy(proxy)
print('代理验证成功')
if __name__ == '__main__':
VerifyProxy().verify_all()