This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.py
156 lines (133 loc) · 5.47 KB
/
main.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
import eventlet
eventlet.monkey_patch()
import argparse, util, dataCenterSpeedtest, sys
import prettytable as pt
parser = argparse.ArgumentParser()
parser.add_argument("--search",
nargs='?',
help="使用Where语句筛选数据中心",
default="",
const="")
parser.add_argument("--order",
nargs='?',
help="使用Order语句对数据中心排序(仅在--list时生效)",
default="",
const="")
parser.add_argument("--list", nargs='+', help="输出筛选后的结果,需要提供key")
parser.add_argument("--speedtest", help="对筛选后的结果测速", action='store_true')
parser.add_argument("--icmping", help="对筛选后的结果进行icmping", action='store_true')
parser.add_argument("--tcping", help="对筛选后的结果进行tcping", action='store_true')
parser.add_argument("--pingCount", default=20, help="发送ping的数量", type=int)
parser.add_argument("--pingTimeout", default=1, help="ping超时时间(秒)", type=int)
parser.add_argument("--flushPing", help="清除ping的相关记录", action='store_true')
parser.add_argument("--flushSpeedtest", help="清除测速的相关记录", action='store_true')
args = parser.parse_args()
conn, cursor = util.loadDatabase()
def handleFlush():
if args.flushPing:
cursor.execute(
"UPDATE data_centers SET ping_loss=0,ping_received=0,ping_time=0 "
+ args.search)
conn.commit()
if args.flushSpeedtest:
cursor.execute("UPDATE data_centers SET download_speed=0 " +
args.search)
conn.commit()
def handlePing():
if args.icmping:
util.go_ping(1, args.search, args.pingCount, args.pingTimeout)
if args.tcping:
util.go_ping(0, args.search, args.pingCount, args.pingTimeout)
def handleSpeedtest():
if args.speedtest:
testFileLinkSet = set()
currentCount = 0
result = cursor.execute(
'SELECT idc,country,city,test_file_link,enable_download_test,download_speed FROM data_centers '
+ args.search).fetchall()
count = len(result)
for row in result:
if row[4] == 1:
if row[3] in testFileLinkSet:
print(
"节点:", row[0] + " " + row[1] +
"({0}/{1})".format(currentCount + 1, count))
print("测试文件地址:", row[3])
print("已测速, 跳过...")
else:
[time, data,
withError] = dataCenterSpeedtest.measureDownloadSpeed(
row[0], row[1] + " " + row[2], row[3], currentCount,
count)
cursor.execute(
"UPDATE data_centers SET download_speed=? WHERE test_file_link=?",
(max(data / time, row[5]), row[3]))
conn.commit()
if not withError:
testFileLinkSet.add(row[3])
else:
print(
"节点:", row[0] + " " + row[1] + " " + row[2] +
"({0}/{1}) 未提供测速链接,无法测速。".format(currentCount + 1, count))
currentCount += 1
ListMap = {
"id": 0,
"idc": 1,
"idc_abbr": 2,
"official_website": 3,
"enable_tcping_test": 4,
"enable_download_test": 5,
"country": 6,
"city": 7,
"ip": 8,
"tcping_port": 9,
"test_file_link": 10,
"ping_loss": 11,
"ping_received": 12,
"ping_time": 13,
"download_speed": 14
}
TranslateMap = [
"序号", "服务商名称", "服务商缩写", "官方网站", "是否可以进行tcping", "是否可以进行测速", "国家", "城市",
"IP地址", "tcping端口", "下载文件链接", "ping丢包", "ping收包", "ping的平均时间", "下载速度"
]
def handleList():
if args.list == None:
return
listArgs = []
for listArg in args.list:
listArgs.append(ListMap[listArg])
listArgs.sort()
results = cursor.execute(
'SELECT *,CASE WHEN ping_received+ping_loss=1 THEN 0.0 ELSE (CAST(ping_loss as REAL)/(ping_received+ping_loss)) END as ping_loss_rate FROM data_centers '
+ args.search + " " + args.order).fetchall()
searchTable = pt.PrettyTable()
fieldNames = []
for listArg in listArgs:
fieldNames.append(TranslateMap[listArg])
searchTable = pt.PrettyTable(fieldNames)
for result in results:
resultArray = []
for listArg in listArgs:
if listArg == 11:
resultArray.append("{}({:.1f}%)".format(
result[listArg],
util.getPercentage(result[11], result[11] + result[12])))
elif listArg == 12:
resultArray.append("{}({:.1f}%)".format(
result[listArg],
util.getPercentage(result[12], result[11] + result[12])))
elif listArg == 13:
resultArray.append("{:.2f}ms".format(result[listArg]))
elif listArg == 14:
resultArray.append(util.prettifyUnit(result[listArg]))
else:
resultArray.append(result[listArg])
searchTable.add_row(resultArray)
print(searchTable)
if len(sys.argv) < 2:
parser.print_usage()
sys.exit(1)
actions = [handleFlush, handlePing, handleSpeedtest, handleList]
for behaviour in actions:
behaviour()