一个简易的Python封装的FTP功能,python封装ftp,这是一个简易的Pytho
一个简易的Python封装的FTP功能,python封装ftp,这是一个简易的Pytho
这是一个简易的Python封装的FTP功能实现的代码示例,未做python注释,但细细分解还是可以理解吸收的。
from ctypes import *import osimport sysimport ftplibclass KANFtp: ftp = ftplib.FTP() bIsDir = False path = "" def __init__(self, host): self.ftp.connect( host ) def Login(self, user, passwd ): self.ftp.login( user, passwd ) print self.ftp.welcome def DownLoadFile( self, LocalFile, RemoteFile ): file_handler = open( LocalFile, 'wb' ) self.ftp.retrbinary( "RETR %s" %( RemoteFile ), file_handler.write ) file_handler.close() return True def UpLoadFile( self, LocalFile, RemoteFile ): if os.path.isfile( LocalFile ) == False: return False file_handler = open( LocalFile, "rb" ) self.ftp.storbinary( 'STOR %s'%RemoteFile, file_handler, 4096 ) file_handler.close() return True def UpLoadFileTree( self, LocalDir, RemoteDir ): if os.path.isdir( LocalDir ) == False: return False LocalNames = os.listdir( LocalDir ) self.ftp.cwd( RemoteDir ) for Local in LocalNames: src = os.path.join( LocalDir, Local) if os.path.isdir( src ): self.UpLoadFileTree( src, Local ) else: self.UpLoadFile( src, Local ) self.ftp.cwd( ".." ) return def DownLoadFileTree( self, LocalDir, RemoteDir ): if os.path.isdir( LocalDir ) == False: os.makedirs( LocalDir ) self.ftp.cwd( RemoteDir ) RemoteNames = self.ftp.nlst() for file in RemoteNames: Local = os.path.join( LocalDir, file ) if self.isDir( file ): self.DownLoadFileTree( Local, file ) else: self.DownLoadFile( Local, file ) self.ftp.cwd( ".." ) return def show( self, list ): result = list.lower().split( " " ) if self.path in result and "<dir>" in result: self.bIsDir = True def isDir( self, path ): self.bIsDir = False self.path = path #this ues callback function ,that will change bIsDir value self.ftp.retrlines( 'LIST', self.show ) return self.bIsDirftp = KANFtp('192.168.21.10')ftp.Login('XXX','XXXX')#www.iplaypy.com#ftp.DownLoadFile('TEST.TXT', 'public\\hechangmin\\TEST.TXT')#ok#ftp.UpLoadFile('TEST.TXT', 'public\\hechangmin\\TEST.TXT')#ok#ftp.DownLoadFileTree('HECM', 'public\\hechangmin\\xxx\\')#okftp.UpLoadFileTree('ts',"Public\\hechangmin\\testFTP" )print "ok!"
编橙之家文章,
相关内容
- 基于Python的词典功能之爱词霸示例,python词霸,基于Py
- Python对RSA算法的简单实现,pythonrsa算法,Python对RSA算法
- 一个初学者练手的Python多线程实现下载的程序,练手
- Python迭代在现实中的应用示例源码,python示例,Python迭代
- Python方法完成自动连接ssh示例,python方法ssh示例,Pytho
- 关于Python解析xml dom的简单应用,python解析xmldom,关于P
- 适用于小公司的订餐系统Python脚本,订餐python,#!/usr/b
- python md5有生成与应用示例,pythonmd5,python md5有生
- 解决Python2不支持datetime的json encode问题,python2json,解决
- Python定期查询域名状态并完成记录的小脚本,python域名
评论关闭