Commit 5d7e1fec by 刘豪

update

parent 0f08e470
Showing with 33 additions and 7 deletions
import ftplib import ftplib
import os
class FTPUtil: class FTPUtil:
...@@ -20,12 +21,37 @@ class FTPUtil: ...@@ -20,12 +21,37 @@ class FTPUtil:
def get_connection(self): def get_connection(self):
return self.ftp return self.ftp
def download(self, remote_path, local_path): def download(self, remote_file, local_file):
""" try:
remote_path means the remote file name # 检查本地文件是否已经存在,如果存在则获取其大小
and local_path is the local file name. local_file_size = 0
""" if os.path.exists(local_file):
with open(local_path, 'wb') as fp: local_file_size = os.path.getsize(local_file)
self.ftp.retrbinary('RETR ' + remote_path, fp.write)
# 设置从指定位置开始下载
self.ftp.voidcmd('TYPE I')
remote_file_size = self.ftp.size(remote_file)
if local_file_size < remote_file_size:
self.ftp.sendcmd('REST {}'.format(local_file_size))
# 以追加模式打开本地文件
with open(local_file, 'ab') as file:
def callback(data):
file.write(data)
# 开始下载文件
self.ftp.retrbinary('RETR {}'.format(remote_file), callback)
print(f'{local_file} 文件下载完成')
except Exception as e:
print(f'下载 {local_file} 过程中出现错误: {e}')
# def download(self, remote_path, local_path):
# """
# remote_path means the remote file name
# and local_path is the local file name.
# """
# with open(local_path, 'wb') as fp:
# self.ftp.retrbinary('RETR ' + remote_path, fp.write)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment