1.增加了是否使用数据库的判断。
This commit is contained in:
parent
68be3a9a64
commit
f9bff910bb
|
|
@ -1,14 +1,14 @@
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
print('Config is loaded.')
|
||||||
g_args = None
|
_g_args = None
|
||||||
json_obj = None
|
_json_obj = None
|
||||||
|
|
||||||
|
|
||||||
def get_servers():
|
def get_servers():
|
||||||
global json_obj
|
global _json_obj
|
||||||
js = json_obj
|
js = _json_obj
|
||||||
_servers = js["servers"]
|
_servers = js["servers"]
|
||||||
servers_dic = {}
|
servers_dic = {}
|
||||||
for s in _servers:
|
for s in _servers:
|
||||||
|
|
@ -19,8 +19,8 @@ def get_servers():
|
||||||
|
|
||||||
|
|
||||||
def read_json():
|
def read_json():
|
||||||
global g_args
|
global _g_args
|
||||||
json_path = g_args.config
|
json_path = _g_args.config
|
||||||
if json_path is None:
|
if json_path is None:
|
||||||
print("specify json path")
|
print("specify json path")
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
@ -30,23 +30,32 @@ def read_json():
|
||||||
|
|
||||||
|
|
||||||
def get_config():
|
def get_config():
|
||||||
global json_obj
|
global _json_obj
|
||||||
return json_obj
|
return _json_obj
|
||||||
|
|
||||||
|
|
||||||
def init_config():
|
def init_config():
|
||||||
global g_args
|
global _g_args
|
||||||
if g_args is not None:
|
if _g_args is not None:
|
||||||
return g_args
|
return _g_args
|
||||||
parser = argparse.ArgumentParser(description="Help")
|
parser = argparse.ArgumentParser(description="Help")
|
||||||
parser.add_argument("-config", type=str)
|
parser.add_argument("-config", type=str)
|
||||||
_args = parser.parse_args()
|
_args = parser.parse_args()
|
||||||
g_args = _args
|
_g_args = _args
|
||||||
global json_obj
|
global _json_obj
|
||||||
json_obj = read_json()
|
_json_obj = read_json()
|
||||||
return _args
|
return _args
|
||||||
|
|
||||||
|
|
||||||
|
def get(_setting):
|
||||||
|
if _setting in setting:
|
||||||
|
return setting[_setting]
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
init_config()
|
||||||
|
setting = _json_obj
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
args = init_config()
|
pass
|
||||||
print(args.config)
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@ from download_speed import config, model
|
||||||
# file_size if byte
|
# file_size if byte
|
||||||
# longest_time seconds
|
# longest_time seconds
|
||||||
def download_file(chunk_size=4 * 1024, file_size=100 * 1024 * 1024, longest_time=60):
|
def download_file(chunk_size=4 * 1024, file_size=100 * 1024 * 1024, longest_time=60):
|
||||||
|
config_file_size = config.get("download_file_size")
|
||||||
|
if config_file_size is not None:
|
||||||
|
file_size = config_file_size
|
||||||
execution_datetime = datetime.datetime.now()
|
execution_datetime = datetime.datetime.now()
|
||||||
print("Download at {time}".format(time=execution_datetime))
|
print("Download at {time}".format(time=execution_datetime))
|
||||||
print(
|
print(
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,15 @@ import datetime
|
||||||
from download_speed import config
|
from download_speed import config
|
||||||
|
|
||||||
config.init_config()
|
config.init_config()
|
||||||
mysql_addr = config.read_json()
|
mysql_addr = config.get('mysql_addr')
|
||||||
mysql_db = MySQLDatabase(
|
mysql_db = MySQLDatabase(
|
||||||
"vps", user="dmy", password="abc123+_", host=mysql_addr, port=3306
|
"vps", user="dmy", password="abc123+_", host=mysql_addr, port=3306
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def add_record(execution_datetime, server, speed):
|
def add_record(execution_datetime, server, speed):
|
||||||
|
if not config.get('mysql_enable'):
|
||||||
|
return
|
||||||
execution_datetime_utc = execution_datetime - datetime.timedelta(
|
execution_datetime_utc = execution_datetime - datetime.timedelta(
|
||||||
hours=8
|
hours=8
|
||||||
) # 转换为UTC时间。
|
) # 转换为UTC时间。
|
||||||
|
|
@ -22,6 +24,8 @@ def add_record(execution_datetime, server, speed):
|
||||||
|
|
||||||
|
|
||||||
def add_ping_record(execution_datetime, server, ping):
|
def add_ping_record(execution_datetime, server, ping):
|
||||||
|
if not config.get('mysql_enable'):
|
||||||
|
return
|
||||||
execution_datetime_utc = execution_datetime - datetime.timedelta(
|
execution_datetime_utc = execution_datetime - datetime.timedelta(
|
||||||
hours=8
|
hours=8
|
||||||
) # 转换为UTC时间。
|
) # 转换为UTC时间。
|
||||||
|
|
|
||||||
6
main.py
6
main.py
|
|
@ -3,11 +3,5 @@
|
||||||
from download_speed import downloader, config
|
from download_speed import downloader, config
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
config.init_config()
|
|
||||||
setting = config.get_config()
|
|
||||||
if "download_file_size" in setting:
|
|
||||||
download_file_size = setting["download_file_size"]
|
|
||||||
downloader.download_file(file_size=download_file_size)
|
|
||||||
else:
|
|
||||||
downloader.download_file()
|
downloader.download_file()
|
||||||
print("Finished.")
|
print("Finished.")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue