-
Notifications
You must be signed in to change notification settings - Fork 1
/
auto_proxy.py
executable file
·229 lines (200 loc) · 7.52 KB
/
auto_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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import os
import logging
import time
import copy
import re
# 这里引入一个日志模块,输入运行的日志信息,就这么写,先不管能不能用
<<<<<<< HEAD
# 我可以在这个里面限定一些条件,比如Avgping小于50 AvgSpeed要大于25MB,设定变量 googleping也就是SitePing要小于600,当然也可以用下面的中位数方法
# avgping_limit = 50
# avgspeed_limit = 25
# siteping_limit = 600
# print("程序开始执行,请检查设定的阈值,设定的是平均ping要求小于%dms,平均速度要求大于%dMbps,google站点访问ping为%dms\n"%(avgping_limit,avgspeed_limit,siteping_limit))
=======
# 我可以在这个里面限定一些条件,比如Avgping小于50 AvgSpeed要大于25MB,设定变量 googleping也就是SitePing要小于600
avgping_limit = 50
avgspeed_limit = 25
siteping_limit = 600
print("程序开始执行,请检查设定的阈值,设定的是平均ping要求小于%dms,平均速度要求大于%dMbps,google站点访问ping为%dms\n"%(avgping_limit,avgspeed_limit,siteping_limit))
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
logging.basicConfig(
filename="proxy.log",
format='%(asctime)s - %(name)s - %(levelname)s - %(module)s: %(message)s',
datefmt="%Y-%m-%d %H:%M:%S %p",
level=10
)
<<<<<<< HEAD
logging.info("logging correct\n")
=======
logging.info("日志正常\n")
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
# print("hello world!")
h1 = logging.FileHandler(filename="proxy.log",encoding="utf-8")
sh = logging.StreamHandler()
# 这里我要实现一个简单的检查本地目录信息,包括results文件夹的存在和找到目标log文件
print(os.getcwd())
<<<<<<< HEAD
if os.path.isdir("stairspeedtest")==True:
print("目标目录stairspeedtest存在,继续检查stairspeedtest log文件存在\n")
else:
print("stairspeedtest文件夹不存在,错误,退出")
exit()
os.chdir("./stairspeedtest/results/")
=======
dir_list=os.listdir(".")
if "results" in dir_list:
print("目标目录results存在,继续检查stairspeedtest log文件存在\n")
os.chdir("./results/")
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
print(os.getcwd())
# 这里我把字符串列表排一下序,不然容易出错,按扩展名排序。
def last_3(elem):
return elem[-3:]
dir_list=os.listdir(".")
dir_list.sort(key=last_3)
# print(dir_list)
# 这里学到了反向遍历的方法,从后往前遍历方便删除特定元素
for i in range(len(dir_list)-1,-1,-1):
if dir_list[i].find("log") == -1:
dir_list.remove(dir_list[i])
<<<<<<< HEAD
#这里需要加个按前几位排序,方便测试环境
def take_y_m_d(elem):
return elem[0:8]
dir_list.sort(key=take_y_m_d)
=======
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
target_file=dir_list[-1]
if target_file.find("log")!=-1:
print(target_file,"\n","已经找到目标文件\n")
else:
print("get target directory error exit\n")
exit()
# 这里可以简单的做一个时间匹配,要求只检索和当天时间相同的结果,太久远的记录不匹配。 开发过程暂时不启用算了
<<<<<<< HEAD
todaytime= time.strftime("%Y%m%d")
=======
'''todaytime= time.strftime("%Y%m%d")
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
if target_file.find(todaytime)!=-1:
print("VALID TIME! CORRECT!\n")
else:
print("INVALID TIME! ERROR!\n")
<<<<<<< HEAD
exit()
=======
exit()'''
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
# 完成了找到目标文件的功能,后面开始进行测速结果文件的查找和匹配 筛选
# file=open("../results/","r",encoding="UTF-8-sig")
# content=file.read(5)
# print(content)
# 我想使用with open这个方法,感觉比open方便一些
with open(target_file,"r",encoding="utf-8") as f:
content=f.read()
# print(content)
data_list=content.split("\n\n")
<<<<<<< HEAD
# 这里很奇怪用for i in data_list无法做到分离
=======
#这里很奇怪用for i in data_list无法做到分离
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
for i in range(len(data_list)):
data_list[i]=data_list[i].split("\n")
# print(data_list)
data_list.remove(data_list[0])
data_list.remove(data_list[-1])
# print(data_list)
<<<<<<< HEAD
# 只保留上述列表的基础数据,其余的都丢了
=======
#只保留上述列表的基础数据,其余的都丢了
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
'''
0 [speed^[大流量]联通→日本dg]
1 AvgPing=40.00
2 AvgSpeed=17.02MB
3 GroupID=0
4 ID=4
5 MaxSpeed=27.37MB
6 Online=true
7 PkLoss=0.00%
8 RawPing=38,36,50,37,37,42
9 RawSitePing=597,606,643,687,639,620,643,715,601,586
10 RawSpeed=0,0,282464,1847960,6362364,6930112,16458046,17046482,31184190,20727160,26330074,31062408,18424742,34994168,21983282,30155760,23708220,18895004,32025384,18876346
11 SitePing=633.70
12 ULSpeed=N/A
13 UsedTraffic=178647083'''
'''[
0'[speed^[主力]移动1→香港gc]',
1 'AvgPing=13.17',
2 'AvgSpeed=49.39MB',
3 'SitePing=306.30'
]'''
print(len(data_list))
data=copy.deepcopy(data_list)
for i in range(len(data_list)-1,-1,-1):
temp=data_list[i][11]
data_list[i]=data_list[i][0:3]
data_list[i].append(temp)
data_list[i][1]
data_list[i][1]=float(data_list[i][1][8:])
data_list[i][3] = float(data_list[i][3][9:])
if data_list[i][2]!="AvgSpeed=N/A":
data_list[i][2]=float(data_list[i][2][9:-3])
else:
data_list.remove(data_list[i])
<<<<<<< HEAD
print(data_list,"\n")
print(len(data_list))
avgping_list=[]
avgspeed_list = []
siteping_list = []
for i in range(len(data_list)-1,-1,-1):
avgping_list.append(data_list[i][1])
avgspeed_list.append(data_list[i][2])
siteping_list.append(data_list[i][3])
avgping_list.sort()
avgspeed_list.sort(reverse=True)
index=[0,1,2]
# 对index的值进行不同的设定,三个参数的权重不同,最低的是直接ping
siteping_list.sort()
index[0]=int((len(data_list)*8)/10)
index[1]=int((len(data_list)*5)/10)
index[2]=int((len(data_list)*4)/10)
print(avgping_list,"\n",avgspeed_list,"\n",siteping_list,"\n",index)
avgping_limit = avgping_list[index[0]]
avgspeed_limit = avgspeed_list[index[1]]
siteping_limit = siteping_list[index[2]]
print("程序开始执行,请检查设定的阈值,设定的是取0.6中位数,平均ping要求小于%dms,平均速度要求大于%dMbps,google站点访问ping小于%dms\n"%(avgping_limit,avgspeed_limit,siteping_limit))
=======
print(data_list)
print(len(data_list))
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
# 下面开始正式的筛选节点
for i in range(len(data_list)-1,-1,-1):
if data_list[i][1]<avgping_limit and data_list[i][2]>avgspeed_limit and data_list[i][3]<siteping_limit:
continue
else:
data_list.remove(data_list[i])
def takesecond(elem):
return elem[2]
data_list.sort(key=takesecond,reverse=1)
print("筛选完成")
<<<<<<< HEAD
# 去掉一些不太靠谱的节点,印度节点
for i in range(len(data_list)-1,-1,-1):
if "印度" in data_list[i][0]:
data_list.remove(data_list[i])
=======
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
print("符合设定阈值的节点个数为%d个,分别是\n"%len(data_list))
for i in data_list:
print(i)
<<<<<<< HEAD
with open("../../node_result.txt","w",encoding="utf-8") as file:
=======
with open("../node_result.txt","w",encoding="utf-8") as file:
>>>>>>> af053dd2118bce219ca9b6592ab550128ad2324d
file.write(str(data_list))
print("结果写入node_result.txt完成")