复制代码 代码如下:
#!/usr/bin/py2
# -*- coding: utf-8 -*-
#encoding=utf-8
'''''
ftp自动下载、自动上传脚本,可以递归目录操作
'''
from ftplib import FTP
import os, sys, string, datetime, time
import socket
class FtpClient:
def __init__(self, host, user, passwd, remotedir, port=21):
self.hostaddr = host
self.username = user
self.password = passwd
self.remotedir = remotedir
self.port = port
self.ftp = FTP()
self.file_list = []
def __del__(self):
self.ftp.close()
def login(self):
ftp = self.ftp
try:
timeout = 60
socket.setdefaulttimeout(timeout)
ftp.set_pasv(True)
ftp.connect(self.hostaddr, self.port)
print 'Connect Success %s' %(self.hostaddr)
ftp.login(self.username, self.password)
print 'Login Success %s' %(self.hostaddr)
debug_print(ftp.getwelcome())
except Exception:
deal_error("Connect Error or Login Error")
try:
ftp.cwd(self.remotedir)
except(Exception):
deal_error('Change Directory Error')
def is_same_size(self, localfile, remotefile):
try:
remotefile_size = self.ftp.size(remotefile)
except:
remotefile_size = -1
try:
localfile_size = os.path.getsize(localfile)
except:
localfile_size = -1
debug_print('lo:%d re:%d' %(localfile_size, remotefile_size),)
if remotefile_size == localfile_size:
return 1
else:
return 0
def download_file(self, localfile, remotefile):
if self.is_same_size(localfile, remotefile):
return
else:
pass
file_handler = open(localfile, 'wb')
self.ftp.retrbinary('RETR %s'%(remotefile), file_handler.write)
file_handler.close()
def download_files(self, localdir='./', remotedir='./'):
try:
self.ftp.cwd(remotedir)
except:
return
if not os.path.isdir(localdir):
os.makedirs(localdir)
self.file_list = []
self.ftp.dir(self.get_file_list)
remotenames = self.file_list
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.ftp.cwd('..')
def upload_file(self, localfile, remotefile):
if not os.path.isfile(localfile):
return
if self.is_same_size(localfile, remotefile):
return
file_handler = open(localfile, 'rb')
self.ftp.storbinary('STOR %s' %remotefile, file_handler)
file_handler.close()
def upload_files(self, localdir='./', remotedir = './'):
if not os.path.isdir(localdir):
return
localnames = os.listdir(localdir)
self.ftp.cwd(remotedir)
for item in localnames:
src = os.path.join(localdir, item)
if os.path.isdir(src):
try:
self.ftp.mkd(item)
except:
debug_print('Directory Exists %s' %item)
self.upload_files(src, item)
else:
self.upload_file(src, item)
self.ftp.cwd('..')
def mkdir(self, remotedir='./'):
try:
self.ftp.mkd(remotedir)
except:
debug_print('Directory Exists %s' %remotedir)
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
def debug_print(str):
print (str)
def deal_error(e):
timenow = time.localtime()
datenow = time.strftime('%Y-%m-%d', timenow)
logstr = '%s Error: %s' %(datenow, e)
debug_print(logstr)
file.write(logstr)
sys.exit()
Python,FTP操作类
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]