1.读取config.json中下载文件的大小。
This commit is contained in:
parent
752cf79006
commit
f1367fdf8a
10
config.py
10
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
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
7
main.py
7
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.")
|
||||
|
|
|
|||
Loading…
Reference in New Issue