python简介FTP模块

Python中的ftplib模块

Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件

FTP的工作流程及基本操作可参考协议RFC959

ftp登陆连接

from ftplib import FTP #加载ftp模块

ftp=FTP() #设置变量
ftp.set_debuglevel(2) #打开调试级别2,显示详细信息
ftp.connect("IP","port") #连接的ftp sever和端口
ftp.login("user","password")#连接的用户名,密码
print ftp.getwelcome() #打印出欢迎信息
ftp.cmd("xxx/xxx") #更改远程目录
bufsize=1024 #设置的缓冲区大小
filename="filename.txt" #需要下载的文件
file_handle=open(filename,"wb").write #以写模式在本地打开文件
ftp.retrbinaly("RETR filename.txt",file_handle,bufsize) #接收服务器上文件并写入本地文件
ftp.set_debuglevel(0) #关闭调试模式
ftp.quit #退出ftp

ftp相关命令操作

ftp.cwd(pathname) #设置FTP当前操作的路径
ftp.dir() #显示目录下文件信息
ftp.nlst() #获取目录下的文件
ftp.mkd(pathname) #新建远程目录
ftp.pwd() #返回当前所在位置
ftp.rmd(dirname) #删除远程目录
ftp.delete(filename) #删除远程文件
ftp.rename(fromname, toname)#将fromname修改名称为toname。
ftp.storbinaly("STOR filename.txt",file_handel,bufsize) #上传目标文件

ftp.retrbinary("RETR filename.txt",file_handel,bufsize)#下载FTP文件

from ctypes import *
import os
import sys
import ftplib

class 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
        print os.path
        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 )
        if self.ftp.dir(RemoteDir) == None:
            self.ftp.mkd( RemoteDir )
        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:
                #print Local, file
                self.DownLoadFile( Local, file )
        self.ftp.cwd( ".." )
        return
    
    def show( self, list  ):
        result = list.lower().split( " " )
        if self.path in result and "1" not in result:
            self.bIsDir = True
            return
     
    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.bIsDir
    
    def dirSize(self, RemoteDir):
        self.size = 0
        self.ftp.cwd( RemoteDir )
        RemoteNames = self.ftp.nlst()
        for file in RemoteNames:
            if self.isDir( file ):
                continue              
            else:
                #print Local, file
                self.size += self.ftp.size( file )
        self.ftp.cwd( ".." )
        return self.size

ftp = KANFtp('192.168.10.21')
ftp.Login('00000b0002','123456')

#ftp.DownLoadFile('SQLyog.dll', '/00000b0002/repos/SQLyog.dll')#ok
#ftp.UpLoadFile('SQLyog.dll', '/00000b0002/repos/SQLyog.dll')#ok
#ftp.DownLoadFileTree('python', '/00000b0002/repos/python')#ok
#ftp.UpLoadFileTree('sss',"/00000b0002/repos/sss" )
print ftp.dirSize('/00000b0002/repos/index.php')
print "ok!"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值