From f1367fdf8ad5d99a38be94ccf408f460653acd75 Mon Sep 17 00:00:00 2001 From: facat Date: Sat, 22 Jun 2019 13:41:50 +0800 Subject: [PATCH] =?UTF-8?q?1.=E8=AF=BB=E5=8F=96config.json=E4=B8=AD?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6=E7=9A=84=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 10 +++++++--- downloader.py | 16 +++++++++------- main.py | 7 ++++++- model.py | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/config.py b/config.py index cd03c00..0c235d7 100644 --- a/config.py +++ b/config.py @@ -3,10 +3,12 @@ import json import sys g_args = None +json_obj = None def get_servers(): - js = read_json() + global json_obj + js = json_obj _servers = js["servers"] servers_dic = {} for s in _servers: @@ -28,8 +30,8 @@ def read_json(): def get_config(): - global g_args - return g_args + global json_obj + return json_obj def init_config(): @@ -40,6 +42,8 @@ def init_config(): parser.add_argument("-config", type=str) _args = parser.parse_args() g_args = _args + global json_obj + json_obj = read_json() return _args diff --git a/downloader.py b/downloader.py index a1cfc78..e1140a9 100644 --- a/downloader.py +++ b/downloader.py @@ -4,14 +4,16 @@ import model import config - - - # file_size if byte # longest_time seconds def download_file(chunk_size=4 * 1024, file_size=100 * 1024 * 1024, longest_time=60): - execution_datetime=datetime.datetime.now() + execution_datetime = datetime.datetime.now() print("Download at {time}".format(time=execution_datetime)) + print( + "Download {MB_file_size}Mbyte file.".format( + MB_file_size=file_size / 1024 / 1024 + ) + ) servers = config.get_servers() for server_name in servers: server = servers[server_name] @@ -19,7 +21,7 @@ def download_file(chunk_size=4 * 1024, file_size=100 * 1024 * 1024, longest_time server_name = server["name"] chunk_read = 0 s_time = datetime.datetime.now() - print('start time:{s_time}'.format(s_time=s_time)) + print("start time:{s_time}".format(s_time=s_time)) duration = longest_time try: with requests.get(url, stream=True, timeout=longest_time) as r: @@ -37,9 +39,9 @@ def download_file(chunk_size=4 * 1024, file_size=100 * 1024 * 1024, longest_time ) ) break - print('end time:{e_time}'.format(e_time=e_time)) + print("end time:{e_time}".format(e_time=e_time)) speed = chunk_read / duration / 1024 / 1024 * 8 # mega bit - model.add_record(execution_datetime,server_name, speed) + model.add_record(execution_datetime, server_name, speed) print("{server}:{speed} Mbit/s".format(server=server_name, speed=speed)) except requests.exceptions.Timeout as e: print( diff --git a/main.py b/main.py index d4edbaf..17efc2c 100644 --- a/main.py +++ b/main.py @@ -5,5 +5,10 @@ import config if __name__ == "__main__": config.init_config() - downloader.download_file() + 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() print("Finished.") diff --git a/model.py b/model.py index b05484b..95d7afb 100644 --- a/model.py +++ b/model.py @@ -2,7 +2,7 @@ from peewee import * import datetime mysql_db = MySQLDatabase( - "vps", user="dmy", password="abc123+_", host="192.168.1.130", port=3306 + "vps", user="dmy", password="abc123+_", host="zero.yoojoo.space", port=3306 )