一、背景
最近公司的一些自动化操作需要使用Python来实现FTP的上传和下载功能。因此参考网上的例子,撸了一段代码来实现了该功能,下面做个记录。
二、ftplib介绍
Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件。
- Python 2.7系列官方文档: https://docs.python.org/2/library/ftplib.html
- Python 3.5系列官方文档:https://docs.python.org/3.5/library/ftplib.html
- Python 3.6系列官方文档:https://docs.python.org/3.6/library/ftplib.html
ftplib中的FTP 主要有以下这些方法
这些方法,可以参考源代码,也可以参考上面贴的官方文档来进行学习。
我们主要需要使用到的api有以下几个:
- 1、连接服务器:
ftp=FTP() #设置变量
ftp.connect(“IP”,”port”) #连接的ftp sever和端口
ftp.login(“user”,”password”)#连接的用户名,密码如果匿名登录则用空串代替即可
- 2、上传文件:
fp=open(‘E:/test.xlsx’,’rb’)
cmd=’STOR filepath/test.xlsx’
ftp.storbinary(cmd, fp)
storbinary是以二进制形式上传文件。
cmd: STOR命令,是FTP的一个命令,后面需要加上保存文件的路径及文件名
fp: 一个打开的文件对象,‘rb’,以二进制形式打开文件
- 3、下载文件:
ftp.nlst(path) #获取目录下的文件
file_handle=open(filename,”wb”).write #以写模式在本地打开文件
ftp.retrbinaly(“RETR filename.txt”,file_handle) #接收服务器上文件并写入本地文件
- 4、其他方法:
ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.set_pasv(0) #0主动模式 1 #被动模式
print ftp.getwelcome() #打印出欢迎信息
ftp.cmd(“xxx/xxx”) #更改远程目录
ftp.set_debuglevel(0) #关闭调试模式
ftp.quit() #退出ftp
ftp.dir() #显示目录下文件信息
ftp.mkd(pathname) #新建远程目录
ftp.pwd() #返回当前所在位置
ftp.rmd(dirname) #删除远程目录
ftp.delete(filename) #删除远程文件
ftp.rename(fromname, toname) #将fromname修改名称为toname。
三、使用ftplib来实现FTP上传下载功能
参考官网以及互联网其他资源,撸了下面这份代码,作为一个FTP工具类
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from ftplib import FTP
import os
import sys
import time
import socket
class MyFTP:
"""
ftp自动下载、自动上传脚本,可以递归目录操作
作者:欧阳鹏
博客地址:http://blog.csdn.net/ouyang_peng/article/details/79271113
"""
def __init__(self, host, port=21):
""" 初始化 FTP 客户端
参数:
host:ip地址
port:端口号
"""
# print("__init__()---> host = %s ,port = %s" % (host, port))
self.host = host
self.port = port
self.ftp = FTP()
# 重新设置下编码方式
self.ftp.encoding = 'gbk'
self.log_file = open("log.txt", "a")
self.file_list = []
def login(self, username, password):
""" 初始化 FTP 客户端
参数:
username: 用户名
password: 密码
"""
try:
timeout = 60
socket.setdefaulttimeout(timeout)
# 0主动模式 1 #被动模式
self.ftp.set_pasv(True)
# 打开调试级别2,显示详细信息
# self.ftp.set_debuglevel(2)
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
def is_same_size(self, local_file, remote_file):
"""判断远程文件和本地文件大小是否一致
参数:
local_file: 本地文件
remote_file: 远程文件
"""
try:
remote_file_size = self.ftp.size(remote_file)
except Exception as err:
# self.debug_print("is_same_size() 错误描述为:%s" % err)
remote_file_size = -1
try:
local_file_size = os.path.getsize(local_file)
except Exception as err:
# self.debug_print("is_same_size() 错误描述为:%s" % err)
local_file_size = -1
self.debug_print('local_file_size:%d , remote_file_size:%d' % (local_file_size, remote_file_size))
if remote_file_size == local_file_size:
return 1
else:
return 0
def download_file(self, local_file, remote_file):
"""从ftp下载文件
参数:
local_file: 本地文件
remote_file: 远程文件
"""
self.debug_print("download_file()---> local_path = %s ,remote_path = %s" % (local_file, remote_file))
if self.is_same_size(local_file, remote_file):
self.debug_print('%s 文件大小相同,无需下载' % local_file)
return
else:
try:
self.debug_print('>>>>>>>>>>>>下载文件 %s ... ...' % local_file)
buf_size = 1024
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
def download_file_tree(self, local_path, remote_path):
"""从远程目录下载多个文件到本地目录
参数:
local_path: 本地路径
remote_path: 远程路径
"""
print("download_file_tree()---> local_path = %s ,remote_path = %s" % (local_path, remote_path))
try:
self.ftp.cwd(remote_path)
except Exception as err:
self.debug_print('远程目录%s不存在,继续...' % remote_path + " ,具体错误描述为:%s" % err)
return
if not os.path.isdir(local_path):
self.debug_print('本地目录%s不存在,先创建本地目录' % local_path)
os.makedirs(local_path)
self.debug_print('切换至目录: %s' % self.ftp.pwd())
self.file_list = []
# 方法回调
self.ftp.dir(self.get_file_list)
remote_names = self.file_list
self.debug_print('远程目录 列表: %s' % remote_names)
for item in remote_names:
file_type = item[0]
file_name = item[1]
local = os.path.join(local_path, file_name)
if file_type == 'd':
print("download_file_tree()---> 下载目录: %s" % file_name)
self.download_file_tree(local, file_name)
elif file_type == '-':
print("download_file()---> 下载文件: %s" % file_name)
self.download_file(local, file_name)
self.ftp.cwd("