python FTP自动下载,自动上传脚本,可以递归目录操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python FTP自动下载,自动上传脚本,可以递归目录操作相关的知识,希望对你有一定的参考价值。
#coding=utf-8
'''
ftp自动下载、自动上传脚本,可以递归目录操作
'''
from ftplib import FTP
import os,sys,string,datetime,time
import socket
class MYFTP:
def __init__(self, hostaddr, username, password, remotedir, port=21):
self.hostaddr = hostaddr
self.username = username
self.password = password
self.remotedir = remotedir
self.port = port
self.ftp = FTP()
self.ftp.set_debuglevel(1)
self.file_list = []
self.dl_files_list = []
# self.ftp.set_debuglevel(2)
def __del__(self):
self.ftp.close()
# self.ftp.set_debuglevel(0)
def changedir(self, directory):
self.ftp.cwd(directory)
self.ftp.voidcmd('TYPE I')#after change the directory the mode will be change to accii mode .so change it to binarymode
def login(self):
ftp = self.ftp
try:
timeout = 300
socket.setdefaulttimeout(timeout)
ftp.set_pasv(True)
print u'开始连接到 %s' %(self.hostaddr)
ftp.connect(self.hostaddr, self.port)
print u'成功连接到 %s' %(self.hostaddr)
print u'开始登录到 %s' %(self.hostaddr)
ftp.login(self.username, self.password)
print u'成功登录到 %s' %(self.hostaddr)
debug_print(ftp.getwelcome())
ftp.voidcmd('TYPE I')
return 1
except Exception:
print u'连接或登录失败'
return 0
try:
self.changedir(self.remotedir)
return 1
except(Exception):
print u'切换目录失败'
return 0
def is_same_size(self, localfile, remotefile):
try:
self.ftp.voidcmd('type i')
remotefile_size = self.ftp.size(remotefile)
except:
remotefile_size = -1
try:
localfile_size = os.path.getsize(localfile)
except:
localfile_size = -1
debug_print('localfile_size:%d remotefile_size:%d' %(localfile_size, remotefile_size),)
if remotefile_size == localfile_size:
return 1
else:
self.remotefile_size = remotefile_size
self.localfile_size = localfile_size
return 0
def download_file(self, localfile, remotefile):
if self.is_same_size(localfile, remotefile):
debug_print(u'%s 文件大小相同,无需下载' %localfile)
return
else:
debug_print(u'>>>>>>>>>>>>下载文件 %s ... ...' %localfile)
#return
save = Savefile(self.remotefile_size, localfile)
self.ftp.retrbinary(u'RETR %s'%(remotefile), save.handler_data)
save.destroy()
self.dl_files_list.append(remotefile);
def download_files(self, localdir='./', remotedir='./'):
try:
self.changedir(remotedir)
except:
debug_print(u'目录%s不存在,继续...' %remotedir)
return
if not os.path.isdir(localdir):
os.makedirs(localdir)
debug_print(u'切换至目录 %s' %self.ftp.pwd())
self.file_list = []
self.ftp.dir(self.get_file_list)
remotenames = self.file_list
#print(remotenames)
#return
for item in remotenames:
filetype = item[0]
filename = item[1]
local = os.path.join(localdir, filename)
if filetype == 'd':
self.download_files(local, filename)
elif filetype == '-':
self.download_file(local, filename)
self.changedir('..')
debug_print(u'返回上层目录 %s' %self.ftp.pwd())
def upload_file(self, localfile, remotefile):
if not os.path.isfile(localfile):
return
if self.is_same_size(localfile, remotefile):
debug_print(u'跳过[相等]: %s' %localfile)
return
file_handler = open(localfile, 'rb')
self.ftp.storbinary('STOR %s' %remotefile, file_handler)
file_handler.close()
debug_print(u'已传送: %s' %localfile)
def upload_files(self, localdir='./', remotedir = './'):
if not os.path.isdir(localdir):
return
localnames = os.listdir(localdir)
self.changedir(remotedir)
for item in localnames:
src = os.path.join(localdir, item)
if os.path.isdir(src):
try:
self.ftp.mkd(item)
except:
debug_print(u'目录已存在 %s' %item)
self.upload_files(src, item)
else:
self.upload_file(src, item)
self.changedir('..')
def get_file_list(self, line):
ret_arr = []
file_arr = self.get_filename(line)
if file_arr[1] not in ['.', '..']:
self.file_list.append(file_arr)
def get_filename(self, line):
pos = line.rfind(':')
while(line[pos] != ' '):
pos += 1
while(line[pos] == ' '):
pos += 1
file_arr = [line[0], line[pos:]]
return file_arr
class Savefile:
def __init__(self, remotefile_size, filename):
self.remotefile_size = remotefile_size
self.localfile_size = 0
self.filename = filename
logstr = "create file " + filename
debug_print(logstr)
self.file_handler = open(filename, 'wb')
self.times = 0
def show_process(self):
self.times += 1
j = '|'
if self.times%2 == 0:
j = '-'
sys.stdout.write(str(int(self.localfile_size*100/self.remotefile_size))+"% -> "+ j +" "+ str(self.localfile_size) + " \r")
sys.stdout.flush()
def handler_data(self, s):
self.localfile_size += len(s)
self.show_process()
self.file_handler.write(s)
if self.times%60 == 0:
self.file_handler.flush()
def destroy(self):
self.file_handler.close()
def debug_print(s):
print s
if __name__ == '__main__':
timenow = time.localtime()
datenow = time.strftime('%Y-%m-%d', timenow)
# 配置如下变量
hostaddr = '******.sw.****.se' # ftp地址
username = '*****' # 用户名
password = '*****' # 密码
# hostaddr = '' # ftp地址
# username = '' # 用户名
# password = '' # 密码
port = 21 # 端口号
rootdir_local = 'C:\\tr\\ru22_pa\\logs' # 本地目录
rootdir_remote = '/RBS' # 远程目录
f = MYFTP(hostaddr, username, password, rootdir_remote, port)
ret = f.login()
if ret != 1:
logstr = u"%s 认证失败退出\n" %datenow
debug_print(logstr)
sys.exit(0)
f.download_files(rootdir_local, rootdir_remote)
timenow = time.localtime()
datenow = time.strftime('%Y-%m-%d', timenow)
logstr = u"%s 成功执行了备份\n" %datenow
debug_print(logstr)
以上是关于python FTP自动下载,自动上传脚本,可以递归目录操作的主要内容,如果未能解决你的问题,请参考以下文章