python简介FTP模块
来源:互联网 发布:小元老师的数学 知乎 编辑:程序博客网 时间:2024/06/05 03:01
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 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 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.sizeftp = 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
- python简介FTP模块
- Python threading模块简介
- python datetime模块 简介
- python 模块简介
- Python os模块简介
- Python sys模块简介
- Python-ConfigParser模块简介
- Python time模块简介
- Python logging 模块简介
- Python CSV模块简介
- Python CSV模块简介
- python os模块简介
- Python 常用模块简介
- python logging模块简介
- Python RE 模块简介
- python ConfigParser模块简介
- python (小)模块简介
- python paramiko模块简介
- HashMap,LinkedHashMap,TreeMap的区别
- c# 文件IO操作 StreamReader StreamWriter Split 使用
- C++模板类头文件和实现文件分离的方法(vs2012测试)
- 团购三巨头承诺“誓言”:诚意够足,效率如何?
- DB_LINK的个人理解
- python简介FTP模块
- 使用 CocoStudio 创建 Cocos2d-x 序列帧和骨骼动画
- 析构函数出错
- Javascript之全局变量
- import javax. media cannot be resolved解决方法
- meta program (1) swap_iter
- 安卓布局基础笔记(相对布局 -- RelativeLayout )
- 张小龙 微信事业群
- leetcode Decode Ways