本文实例讲述了python实现支持目录FTP上传下载文件的方法。分享给大家供大家参考。具体如下:
该程序支持ftp上传下载文件和目录、适用于windows和linux平台。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ftplib
import os
import sys
class FTPSync(object):
conn = ftplib.FTP()
def __init__(self,host,port=21):
self.conn.connect(host,port)
def login(self,username,password):
self.conn.login(username,password)
self.conn.set_pasv(False)
print self.conn.welcome
def test(self,ftp_path):
print ftp_path
print self._is_ftp_dir(ftp_path)
#print self.conn.nlst(ftp_path)
#self.conn.retrlines( 'LIST ./a/b')
#ftp_parent_path = os.path.dirname(ftp_path)
#ftp_dir_name = os.path.basename(ftp_path)
#print ftp_parent_path
#print ftp_dir_name
def _is_ftp_file(self,ftp_path):
try:
if ftp_path in self.conn.nlst(os.path.dirname(ftp_path)):
return True
else:
return False
except ftplib.error_perm,e:
return False
def _ftp_list(self, line):
list = line.split(' ')
if self.ftp_dir_name==list[-1] and list[0].startswith('d'):
self._is_dir = True
def _is_ftp_dir(self,ftp_path):
ftp_path = ftp_path.rstrip('/')
ftp_parent_pat