使用Python编写实现FTP文件的下载
代码
from ftplib import FTP
import os
import sys
import time
import socket
import yaml
import configparser
class MyFTP:
def __init__(self, host, port=21):
""" 初始化 FTP 客户端
参数:
host:ip地址
port:端口号
"""
self.host = host
self.port = port
self.ftp = FTP()
# 重新设置下编码方式
self.ftp.encoding = 'utf-8'
self.log_file = open("log.txt", "a")
self.file_list = []
""" 初始化 FTP 客户端
参数:
username: 用户名
password: 密码
"""
def login(self, username, password):
try:
timeout = 60
socket.setdefaulttimeout(timeout)
# 0主动模式 1 #被动模式
self.ftp.set_pasv(False)
self.debug_print('开始尝试连接到 %s' % self.host)
self.ftp.connect(self.host, self.port)
self.debug_print('成功连接到 %s' % self.host)
self.debug_print('开始尝试登录到 %s' % self.host)
self.ftp.login(username, password)
self.debug_print('成功登录到 %s' % self.host)
self.debug_print(self.ftp.welcome)
except Exception as err:
self.deal_error("FTP 连接或登录失败 ,错误描述为:%s" % err)
pass
""" 处理错误异常
参数:
e:异常
"""
def deal_error(self, e):
log_str = '发生错误: %s' % e
self.write_log(log_str)
sys.exit()
def write_log(self, log_str):
""" 记录日志
参数:
log_str:日志
"""
time_now = time.localtime()
date_now = time.strftime('%Y-%m-%d', time_now)
format_log_str = "%s ---> %s \n " % (date_now, log_str)
print(format_log_str)
self.log_file.write(format_log_str)
"""从远程目录下载多个文件到本地目录
参数:
local_path: 本地路径
remote_path: 远程路径
"""
def download_file_tree(self, local_path, remote_path):
try:
print("download_file_tree()---> local_path = %s ,remote_path = %s" % (local_path, remote_path))
if not os.path.isdir(local_path):
self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
os.makedirs(local_path)
try:
os.chdir(local_path)
self.ftp.cwd(remote_path)
except Exception as err:
self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
return
self.debug_print('切换至目录: %s' % self.ftp.pwd())
self.file_list = []
# 方法回调
self.ftp.dir(self.get_file_list)
except UnicodeDecodeError as err:
print(err)
pass
finally:
remote_names = self.file_list
self.debug_print('远程目录 列表: %s' % remote_names)
for item in remote_names:
file_type = item[0]
file_name = item[1]
if ' ' in file_name:
file_name = file_name[file_name.index(' '):].strip()
local = os.path.join(local_path, file_name)
print('文件名{0},是否是文件:{1},文件类型:{2}'.format(file_name, '.' in file_name, file_type))
print(local)
if '.' in file_name: # 文件
print(remote_path + ",download_file()---> 下载文件: %s" % file_name)
self.download_file(local, remote_path + '/' + file_name)
else: # 目录
print(remote_path + ",download_file_tree()---> 下载目录: %s" % file_name + "/")
self.download_file_tree(local, remote_path + '/' + file_name)
self.ftp.cwd("..")
self.debug_print('返回上层目录 %s' % self.ftp.pwd())
return True
"""从ftp下载文件
参数:
local_file: 本地文件
remote_file: 远程文件
"""
def download_file(self, local_file, remote_file):
self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
try:
self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
buf_size = 8024
file_handler = open(local_file, 'wb')
self.ftp.retrbinary('RETR %s' % remote_file, file_handler.write, buf_size)
file_handler.close()
except Exception as err:
self.debug_print('下载文件出错,出现异常:%s ' % err)
return
if __name__ == "__main__":
# 读取配置文件config.ini
cf = configparser.ConfigParser()
cf.read('config.ini')
cf.options('FTP-Config')
ftp_ip = cf.get('FTP-Config', 'ftp_ip')
user_name = cf.get('FTP-Config', 'user_name')
password = cf.get('FTP-Config', 'password')
client_path = cf.get('FTP-Config', 'client_path')
ftp_path = cf.get('FTP-Config', 'ftp_path')
# 初始化
my_ftp = MyFTP(str(ftp_ip))
# 连接并登录
my_ftp.login(str(user_name), str(password))
# 下载目录
my_ftp.download_file_tree(str(client_path), str(ftp_path))
my_ftp.close()
sys.exit()
配置文件(config.ini)
[FTP-Config]
ftp_ip = 102.108.10.30
user_name = LDEYcs
password = LDEYcs
client_path = D:\landi
ftp_path = /DP/landi
遇到的问题:ftp下载编码问题(UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xcf in position 2: inva)
解决:刚开始用的是读取yml文件,后来改成读取ini文件解决
最初读取文件的方法:
fileName = "config.yml"
f = open(fileName, encoding='utf-8')
address = f.read()
yml = yaml.load(address, Loader=yaml.FullLoader)
最后使用的读取方式:
cf = configparser.ConfigParser()
cf.read('config.ini')
cf.options('FTP-Config')
ftp_ip = cf.get('FTP-Config', 'ftp_ip')
user_name = cf.get('FTP-Config', 'user_name')
password = cf.get('FTP-Config', 'password')
client_path = cf.get('FTP-Config', 'client_path')
ftp_path = cf.get('FTP-Config', 'ftp_path')
打包
pyinstaller -F ***.py