ftplib模块介绍
来源:互联网 发布:家庭收支记账软件 编辑:程序博客网 时间:2024/06/05 09:31
Python使用ftplib制作简易FTP客户端。
#!/usr/bin/python#-*- coding:utf-8 -*-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.dir() #显示目录下文件信息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完整实例
#coding:utf-8from ctypes import *import osimport sysimport ftplibclass myFtp: ftp = ftplib.FTP() bIsDir = False path = "" def __init__(self, host, port='21'): self.ftp.set_debuglevel(2) #打开调试级别2,显示详细信息 #self.ftp.set_pasv(0) #0主动模式 1 #被动模式 self.ftp.connect( host, port ) 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 ) print 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: 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.bIsDir def close( self ): self.ftp.quit()ftp = myFtp('********')ftp.Login('*****','*****')#ftp.DownLoadFile('TEST.TXT', 'others\\runtime.log')#ok#ftp.UpLoadFile('runtime.log', 'others\\runtime.log')#ok#ftp.DownLoadFileTree('bcd', 'others\\abc')#ok#ftp.UpLoadFileTree('aaa',"others\\" )ftp.close()print "ok!"
- ftplib模块介绍
- ftplib模块
- ftplib模块
- ftplib模块
- python的ftplib模块
- Python ftplib模块
- Python中的ftplib模块
- Python ftplib模块
- python ftplib模块
- python的学习---- ftplib模块 .
- ftplib
- ftplib模块--编写ftp客户端程序
- python的学习(十三)---- ftplib模块
- Python中的ftplib模块调用与测试
- python ftplib模块 ftplib.error_perm: 550 SIZE not allowed in ASCII mode错误
- python 利用ftplib模块 实现ftp上传下载代码
- 用python的ftplib模块编写ftp客户端程序
- 用python的ftplib模块编写ftp客户端程序
- 由于短期理财基金到期的最后一天是按活期利率盘算
- C++ 第十周任务-分数求和3
- JavaScript单元测试框架介绍
- android 获取硬件的ID
- sipp -- help 参数与在线帮助
- ftplib模块介绍
- OpenCL和CUDA的使用比较
- 跨站脚本攻击(csrf)的原理与防范
- string类的方法
- 乘法表
- 达芬奇系列DSP——CCS_V5安装技术文档
- 查找即将过期的短域名
- 2.4G射频蓝牙4.0协议芯片NRF51822介绍
- JavaScript单元测试之Qunit