时间:2021-05-22
复制代码 代码如下:
#!/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()
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Python基于FTP模块实现ftp文件上传操作。分享给大家供大家参考,具体如下:#!/usr/bin/python#-*-coding:utf-
本文实例讲述了C#实现自定义FTP操作封装类。分享给大家供大家参考。具体如下:这个C#类封装了FTP的常用操作,包括连接ftp服务器、列表服务器上的目录和文件,
python2.7复制代码代码如下:wgethttp://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
这是一个用于操作FTP的客户端C#类,类已经封装好了各种常用的Ftp操作方法,调用非常简单,你不需要关心ftp连接和操作的细节,只要调用这个类里的相关方法就可以
本文针对PHP操作FTP类进行详细介绍,php实现FTP上传、FTP下载、FTP移动、FTP创建等,供大家参考,具体内容如下1.使用PHP操作FTP-用法2.F