服务端
lib 文件夹下 main.py
def _get(self, header_dir):
"""从服务端下载文件"""
filename = header_dir.get("filename")
full_path = os.path.join(self.current_dir, filename)
if os.path.isfile(full_path):
total_size = os.path.getsize(full_path)
self.send_response(status_code=400, total_size=total_size, current_dir = self.current_dir)
with open(full_path, "rb") as f:
for line in f:
self.conn.send(line)
else:
self.send_response(status_code=401)
def _re_get(self,header_dic):
"""下载断点续存"""
filename = header_dic.get("filename")
current_dir = header_dic.get("current_dir")
total_size = header_dic.get("total_size")
recv_size = header_dic.get("recv_size")
full_path = os.path.join(current_dir, filename)
print(full_path)
if os.path.isfile(full_path):
if os.path.getsize(full_path) == total_size:
self.send_response(status_code=402)
with open(full_path, "rb") as f:
f.seek(recv_size)
for line in f:
self.conn.send(line)
else:
self.send_response(status_code=401)
else:
self.send_response(status_code=401)
客户端
client文件夹下 FTPClient.py 代码 interactive函数 增加部分
def interactive(self):
"""交互指令"""
if self.auth():
self.unfinished_download_check()
......
client文件夹下 FTPClient.py
def unfinished_download_check(self):
"""下载断点续存功能"""
if list(self.shelve_obj_download.keys()):
print("-------Unfinished download list------")
for index, filename in enumerate(self.shelve_obj_download.keys()):
print("%s %s %s" % (index, filename, self.shelve_obj_download[filename][1]))
while True:
choice = input("[select file index to re-get]>>: ").strip()
if not choice: continue
if choice == "back": break
if choice.isdigit():
choice = int(choice)
if choice <= index:
filename = list(self.shelve_obj_download.keys())[choice]
current_dir = self.shelve_obj_download[filename][0]
total_size = self.shelve_obj_download[filename][1]
recv_size = os.path.getsize("%s.download" % filename)
self.create_header_send(action_type="re_get", filename=filename, current_dir=current_dir, total_size=total_size, recv_size=recv_size)
response = self.get_response()
if response.get("status_code") == 402:
progress_bar = self.progress_bar(total_size=total_size, res_size=recv_size, last_size=recv_size)
progress_bar.__next__()
with open("%s.download" % filename, "ab") as f:
while recv_size < total_size:
data = self.client.recv(self.MSG_SIZE)
recv_size += len(data)
progress_bar.send(recv_size)
f.write(data)
else:
print("\n")
print("file %s has re-downloaded !" % filename)
if os.path.isfile(filename):
os.rename("%s.download" % filename, "%s.%s" % (filename, str(time.time())))
else:
os.rename("%s.download" % filename, filename)
del self.shelve_obj_download[filename]
else:
print(response.get("status_msg"))
del self.shelve_obj_download[filename]
else:
print("choice does exist !")
else:
print("please supply number !")
def progress_bar(self, total_size, res_size=0, last_size=0):
"""进度条功能"""
while True:
res_size = yield total_size
percent = int(res_size / total_size * 100)
if res_size > last_size:
print("#" * int(percent / 2) + "{percent}%".format(percent=percent), end="\r", flush=True) # "\\r" 表示打印时覆盖已打印的内容
last_size = res_size
def get(self, command):
"""从服务端下载文件"""
if self.parameter_length_judgment(command, most_size=1):
filename = command[0]
self.create_header_send(action_type="get", filename=filename)
response = self.get_response()
if response.get("status_code") == 400:
total_size = response.get("total_size")
self.current_dir = response.get("current_dir")
progress_bar = self.progress_bar(total_size=total_size)
progress_bar.__next__()
self.shelve_obj_download[filename] = (self.current_dir, total_size)
res_size = 0
with open("%s.download" % filename,"wb") as f:
while res_size < total_size:
data = self.client.recv(self.MSG_SIZE)
res_size += len(data)
progress_bar.send(res_size)
f.write(data)
else:
print("\n")
print("file %s has downloaded !" % filename)
if os.path.isfile(filename):
os.rename("%s.download" % filename, "%s.%s" % (filename, str(time.time())))
else:
os.rename("%s.download" % filename, filename)
del self.shelve_obj_download[filename]
else:
print(response.get("status_msg"))
运行结果
终端命令结果
运行后项目目录