python ftplib模块使用代码实例
这篇文章主要介绍了pythonftplib模块使用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Python中默认安装的ftplib模块定义了FTP类,可用来实现简单的ftp客户端,用于上传或下载文件。
ftp登陆连接
fromftplibimportFTP#加载ftp模块 ftp=FTP()#设置变量 ftp.set_debuglevel(2)#打开调试级别2,显示详细信息 ftp.connect("10.126.64.14",21)#连接的ftpsever和端口 ftp.login("usr_esop","PWD4ftp@201903#")#连接的用户名,密码 print(ftp.getwelcome())#打印出欢迎信息 ftp.cwd("/ZS_ESOP/55-548410/-/zh/Split")#更改远程目录 bufsize=1024#设置的缓冲区大小 filename="tslint.json"#需要下载的文件 file_handle=open(filename,"wb").write#以写模式在本地打开文件 ftp.retrbinary("RETRtslint.json",file_handle,bufsize)#接收服务器上文件并写入本地文件 ftp.set_debuglevel(0)#关闭调试模式 ftp.quit#退出ftp
ftp相关命令操作
ftp.cwd(pathname)#设置FTP当前操作的路径,cwd可以使用“..”,但不使用"./path"以及"../path"这样的相对路径 ftp.dir()#显示目录下文件信息 ftp.nlst()#获取目录下的文件 ftp.mkd(pathname)#新建远程目录 ftp.pwd()#返回当前所在位置 ftp.rmd(dirname)#删除远程目录 ftp.delete(filename)#删除远程文件 ftp.rename(fromname,toname)#将fromname修改名称为toname。可以带路径,起到移动文件的作用 ftp.storbinaly("STORfilename.txt",file_handel,bufsize)#以BINARY模式上传目标文件ftp.storlines("STORfilename.txt",file_handel)#以ASCII模式存储文件ftp.retrbinary("RETRfilename.txt",file_handel,bufsize)#以BINARY模式下载FTP文件FTP.retrlines("RETRfilename.txt",file_handel)#以ASCII模式获取文件或者文件夹列表
FTP.quit()与FTP.close()的区别
- FTP.quit():发送QUIT命令给服务器并关闭掉连接。这是一个比较“缓和”的关闭连接方式,但是如果服务器对QUIT命令返回错误时,会抛出异常。
- FTP.close():单方面的关闭掉连接,不应该用在已经关闭的连接之后,例如不应用在FTP.quit()之后。
下载、上传文件
fromftplibimportFTP defftpconnect(host,username,password): ftp=FTP() #ftp.set_debuglevel(2)#打开调试级别2,显示详细信息ftp.encoding='GB2312'#解决中文编码问题,默认是latin-1,如果将encoding='utf-8'报错,可以改为GB2312、gbk、ISO-8859-1ftp.connect(host,21)#连接 ftp.login(username,password)#登录,如果匿名登录则用空串代替即可 returnftp defdownloadfile(ftp,remotepath,localpath): bufsize=1024#设置缓冲块大小 fp=open(localpath,'wb')#以写模式在本地打开文件 ftp.retrbinary('RETR'+remotepath,fp.write,bufsize)#接收服务器上文件并写入本地文件 ftp.set_debuglevel(0)#关闭调试 fp.close()#关闭文件 defuploadfile(ftp,remotepath,localpath): bufsize=1024 fp=open(localpath,'rb') ftp.storbinary('STOR'+remotepath,fp,bufsize)#上传文件 ftp.set_debuglevel(0) fp.close()#关闭文件 if__name__=="__main__": ftp=ftpconnect("******","***","***") downloadfile(ftp,"***","***") uploadfile(ftp,"***","***") ftp.quit()
上传、下载文件/目录
注:目录内为文件,若为目录则无法传输
importos importftplib classmyFtp: 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.encoding='GB2312'#解决中文编码问题,默认是latin-1,如果将encoding='utf-8'报错,可以改为GB2312、gbk、ISO-8859-1self.ftp.connect(host,port) deflogin(self,user,passwd): self.ftp.login(user,passwd) print(self.ftp.welcome) defdown_load_file(self,local_file,remote_file): file_handler=open(local_file,'wb') self.ftp.retrbinary("RETR%s"%remote_file,file_handler.write) file_handler.close() returnTrue defup_load_file(self,local_file,remote_file): ifnotos.path.isfile(local_file): returnFalse file_handler=open(local_file,"rb") self.ftp.storbinary('STOR%s'%remote_file,file_handler,4096) file_handler.close() returnTrue defup_load_file_tree(self,local_dir,remote_dir): ifnotos.path.isdir(local_dir): returnFalse #print("local_dir:",local_dir) local_names=os.listdir(local_dir) #print("list:",local_names) #print(remote_dir) self.ftp.cwd(remote_dir) forLocalinlocal_names: src=os.path.join(local_dir,Local) ifos.path.isdir(src): self.up_load_file_tree(src,Local) else: self.up_load_file(src,Local) self.ftp.cwd("..") return defdown_load_file_tree(self,local_dir,remote_dir): print("remote_dir:",remote_dir) ifnotos.path.isdir(local_dir): os.makedirs(local_dir) self.ftp.cwd(remote_dir) remote_names=self.ftp.nlst() #print("remote_names",remote_names) #print(self.ftp.nlst(remote_dir)) forfileinremote_names: local=os.path.join(local_dir,file) ifself.is_dir(file): self.down_load_file_tree(local,file) else: self.down_load_file(local,file) self.ftp.cwd("..") return defshow(self,list): result=list.lower().split("") ifself.pathinresultand""inresult: self.bIsDir=True defis_dir(self,path): self.bIsDir=False self.path=path #thisuescallbackfunction,thatwillchangebIsDirvalue self.ftp.retrlines('LIST',self.show) returnself.bIsDir defclose(self): self.ftp.quit() if__name__=="__main__": ftp=myFtp('10.126.64.14',21) ftp.login("usr_esop","PWD4ftp@201903#") ftp.down_load_file_tree(r"C:\PycharmProjects\untitled\backup","/ZS_ESOP/55-548410/-/zh/Split")#ok ftp.up_load_file_tree(r"C:\PycharmProjects\untitled\backup","/ZS_ESOP/55-548410/-/zh/Split")#ok ftp.close() print("ok!")
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。