-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedtest_thread.py
194 lines (178 loc) · 7 KB
/
speedtest_thread.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import logging
import time
import sys
import os
import configloader
import importloader
from speedtest import speedtest
from shadowsocks import common, shell
class Speedtest(object):
def __init__(self):
import threading
self.event = threading.Event()
self.has_stopped = False
def speedtest_thread(self):
if self.event.wait(600):
return
logging.info("Speedtest starting...You can't stop right now!")
CTid = 0
speedtest_ct = speedtest.Speedtest()
speedtest_ct.get_servers()
servers_list = []
for _, servers in sorted(speedtest_ct.servers.items()):
for server in servers:
if server['country'].find(
'China') != -1 and server['sponsor'].find('Telecom') != -1:
servers_list.append(server)
speedtest_ct.get_best_server(servers_list)
results_ct = speedtest_ct.results
CTPing = str(results_ct.server['latency']) + ' ms'
speedtest_ct.download()
CTDLSpeed = str(
round(
(results_ct.download / 1000 / 1000),
2)) + " Mbit/s"
speedtest_ct.upload()
CTUpSpeed = str(
round(
(results_ct.upload / 1000 / 1000),
2)) + " Mbit/s"
CUid = 0
speedtest_cu = speedtest.Speedtest()
speedtest_cu.get_servers()
servers_list = []
for _, servers in sorted(speedtest_cu.servers.items()):
for server in servers:
if server['country'].find(
'China') != -1 and server['sponsor'].find('Unicom') != -1:
servers_list.append(server)
speedtest_cu.get_best_server(servers_list)
results_cu = speedtest_cu.results
CUPing = str(results_cu.server['latency']) + ' ms'
speedtest_cu.download()
CUDLSpeed = str(
round(
(results_cu.download / 1000 / 1000),
2)) + " Mbit/s"
speedtest_cu.upload()
CUUpSpeed = str(
round(
(results_cu.upload / 1000 / 1000),
2)) + " Mbit/s"
CMid = 0
speedtest_cm = speedtest.Speedtest()
speedtest_cm.get_servers()
servers_list = []
for _, servers in sorted(speedtest_cm.servers.items()):
for server in servers:
if server['country'].find(
'China') != -1 and server['sponsor'].find('Mobile') != -1:
servers_list.append(server)
speedtest_cm.get_best_server(servers_list)
results_cm = speedtest_cm.results
CMPing = str(results_cu.server['latency']) + ' ms'
speedtest_cm.download()
CMDLSpeed = str(
round(
(results_cm.download / 1000 / 1000),
2)) + " Mbit/s"
speedtest_cm.upload()
CMUpSpeed = str(
round(
(results_cm.upload / 1000 / 1000),
2)) + " Mbit/s"
if configloader.get_config().API_INTERFACE == 'modwebapi':
webapi.postApi('func/speedtest',
{'node_id': configloader.get_config().NODE_ID},
{'data': [{'telecomping': CTPing,
'telecomeupload': CTUpSpeed,
'telecomedownload': CTDLSpeed,
'unicomping': CUPing,
'unicomupload': CUUpSpeed,
'unicomdownload': CUDLSpeed,
'cmccping': CMPing,
'cmccupload': CMUpSpeed,
'cmccdownload': CMDLSpeed}]})
else:
import cymysql
if configloader.get_config().MYSQL_SSL_ENABLE == 1:
conn = cymysql.connect(
host=configloader.get_config().MYSQL_HOST,
port=configloader.get_config().MYSQL_PORT,
user=configloader.get_config().MYSQL_USER,
passwd=configloader.get_config().MYSQL_PASS,
db=configloader.get_config().MYSQL_DB,
charset='utf8',
ssl={
'ca': configloader.get_config().MYSQL_SSL_CA,
'cert': configloader.get_config().MYSQL_SSL_CERT,
'key': configloader.get_config().MYSQL_SSL_KEY})
else:
conn = cymysql.connect(
host=configloader.get_config().MYSQL_HOST,
port=configloader.get_config().MYSQL_PORT,
user=configloader.get_config().MYSQL_USER,
passwd=configloader.get_config().MYSQL_PASS,
db=configloader.get_config().MYSQL_DB,
charset='utf8')
conn.autocommit(True)
cur = conn.cursor()
cur.execute(
"INSERT INTO `speedtest` (`id`, `nodeid`, `datetime`, `telecomping`, `telecomeupload`, `telecomedownload`, `unicomping`, `unicomupload`, `unicomdownload`, `cmccping`, `cmccupload`, `cmccdownload`) VALUES (NULL, '" +
str(
configloader.get_config().NODE_ID) +
"', unix_timestamp(), '" +
CTPing +
"', '" +
CTUpSpeed +
"', '" +
CTDLSpeed +
"', '" +
CUPing +
"', '" +
CUUpSpeed +
"', '" +
CUDLSpeed +
"', '" +
CMPing +
"', '" +
CMUpSpeed +
"', '" +
CMDLSpeed +
"')")
cur.close()
conn.close()
logging.info("Speedtest finished")
@staticmethod
def thread_db(obj):
if configloader.get_config().SPEEDTEST == 0:
return
if configloader.get_config().API_INTERFACE == 'modwebapi':
import webapi_utils
global webapi
webapi = webapi_utils.WebApi()
global db_instance
db_instance = obj()
try:
while True:
try:
db_instance.speedtest_thread()
except Exception as e:
import traceback
trace = traceback.format_exc()
logging.error(trace)
#logging.warn('db thread except:%s' % e)
if db_instance.event.wait(configloader.get_config().SPEEDTEST * 3600):
break
if db_instance.has_stopped:
break
except KeyboardInterrupt as e:
pass
db_instance = None
@staticmethod
def thread_db_stop():
global db_instance
db_instance.has_stopped = True
db_instance.event.set()