__author__ = 'call server'
__appname__ = "ftp_client"
__version__ = "1.0"
import ftplib
from socket import _GLOBAL_DEFAULT_TIMEOUT
import os
host = '127.0.0.1'
username = 'bl'
password = '123456'
class Ftp(ftplib.FTP):
def __init__(self, host='127.0.0.1', user='bl', passwd='123456', acct='', timeout=_GLOBAL_DEFAULT_TIMEOUT):
ftplib.FTP.__init__(self, host, user, passwd, acct, timeout)
print self.getwelcome()
self.__ftp_dirs = None
self.__ftp_files = None
def __updatePath(self):
self.__ftp_dirs = []
self.__ftp_files = []
for file in self.nlst():
try:
self.cwd(file)
self.cwd('..')
self.__ftp_dirs.append(file.decode('utf-8'))
except:
self.__ftp_files.append(file.decode('utf-8'))
def getPath(self):
self.__updatePath()
return self.__ftp_dirs, self.__ftp_files
def downloadFiles(self, ftp_files, local_path, blocksize=8192):
for ftp_file in ftp_files:
self.__download(ftp_file, local_path, blocksize)
def uploadFiles(self, local_files, ftp_path, blocksize=8192):
for local_file in local_files:
self.__upload(local_file, ftp_path, blocksize)
def __download(self, ftp_file, local_path, blocksize=8192):
local_file = os.path.join(local_path, os.path.basename(ftp_file))
u8_ftp_file = ftp_file.encode('utf-8')
try:
print 'begin __downLoad ' + ftp_file
fp = open(local_file, 'wb')
self.retrbinary('RETR %s' %(u8_ftp_file), fp.write, blocksize)
fp.close()
print '__download ' + ftp_file + ' is end'
except:
print '__download ' + ftp_file + 'is fail'
def __upload(self, local_file, ftp_path, blocksize=8192):
try:
print 'begin __upload ' + local_file
u8_ftp_file = os.path.join(ftp_path, os.path.basename(local_file)).decode('utf-8')
fp = open(local_file, 'rb')
self.storbinary('STOR %s' %(u8_ftp_file), fp, blocksize)
fp.close()
print '__upload ' + local_file + ' is end'
except:
print '__upload ' + local_file + 'is fail'
if __name__ == '__main__':
ftp = Ftp()
dirs, files = ftp.getPath()
pass