Python使用FTP上传下载
class FtpTools():
def _init_(self, host, user, passwd):
self.ftp = ftplib.FTP()
self.ftp.connect('host',21)
self.ftp.login('logback', 'logback')
def upLoad(self, srcFile, dstDir):
try:
#调试级别: 2-显示详细信息;0-关闭调试信息
ftp.set_debuglevel(2)
#选择操作目录
#ftp.cwd('xxx/xxx/')
file_handler = open(srcFile, 'rb')
bufsize = 1024
self.ftp.storbinary("STOR %s" % os.path.basename(dstDir), file_handler, bufsize)
ftp.set_debuglevel(0)
file_handler.close()
except Exception, e:
return 'upLoad file[' + srcFile + '] to [' + dstDir +'] failed! result is: ' + str(e)
return 0
def downLoad(self, srcFile, dstDir):
try:
#调试级别: 2-显示详细信息;0-关闭调试信息
ftp.set_debuglevel(2)
#选择操作目录
#ftp.cwd('xxx/xxx/')
file_handler = open(srcFile, 'rb')
bufsize = 1024
self.ftp.storbinary("RETR %s" % os.path.basename(dstDir), file_handler, bufsize)
ftp.set_debuglevel(0)
file_handler.close()
except Exception, e:
return 'download file[' + srcFile + '] to [' + dstDir +'] failed! result is: ' + str(e)
return 0
def quit(self):
self.ftp.quit()