免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1473 | 回复: 0
打印 上一主题 下一主题

关于ftplib的一些问题 [复制链接]

论坛徽章:
2
数据库技术版块每日发帖之星
日期:2015-08-03 06:20:00IT运维版块每日发帖之星
日期:2015-08-07 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-07-22 18:12 |只看该作者 |倒序浏览
本帖最后由 linuxforlive 于 2013-07-22 18:12 编辑
  1. from ctypes import *

  2. import os  

  3. import sys  

  4. import ftplib  

  5.    

  6. class KANFtp:  

  7.      ftp = ftplib.FTP()  

  8.      bIsDir = False

  9.      path = ""  

  10.      def __init__(self, host):  

  11.          self.ftp.connect( host )  

  12.                

  13.      def Login(self, user, passwd ):  

  14.          self.ftp.login( user, passwd )  
  15.          self.ftp.set_pasv(0)
  16.          print self.ftp.welcome  

  17.      def DownLoadFile( self, LocalFile, RemoteFile ):  

  18.          file_handler = open( LocalFile, 'wb' )  

  19.          self.ftp.retrbinary( "RETR %s" %( RemoteFile ), file_handler.write )   

  20.          file_handler.close()  

  21.          return True

  22.         

  23.      def UpLoadFile( self, LocalFile, RemoteFile ):  

  24.          if os.path.isfile( LocalFile ) == False:  

  25.              return False

  26.          file_handler = open( LocalFile, "rb" )  

  27.          self.ftp.storbinary( 'STOR %s'%RemoteFile, file_handler,  )  

  28.          file_handler.close()  

  29.          return True

  30.    

  31.      def UpLoadFileTree( self, LocalDir, RemoteDir ):  

  32.          if os.path.isdir( LocalDir ) == False:  

  33.              return False

  34.          LocalNames = os.listdir( LocalDir )  

  35.          self.ftp.cwd( RemoteDir )  

  36.          for Local in LocalNames:  

  37.              src = os.path.join( LocalDir, Local)  

  38.              if os.path.isdir( src ):  

  39.                  self.UpLoadFileTree( src, Local )  

  40.              else:  

  41.                  self.UpLoadFile( src, Local )  

  42.                     

  43.          self.ftp.cwd( ".." )  

  44.          return

  45.         

  46.      def DownLoadFileTree( self, LocalDir, RemoteDir ):  

  47.          if os.path.isdir( LocalDir ) == False:  

  48.              os.makedirs( LocalDir )  

  49.          self.ftp.cwd( RemoteDir )  

  50.          RemoteNames = self.ftp.nlst()   

  51.          for file in RemoteNames:  

  52.              Local = os.path.join( LocalDir, file )  

  53.              if self.isDir( file ):  

  54.                  self.DownLoadFileTree( Local, file )                  

  55.              else:  

  56.                  self.DownLoadFile( Local, file )  

  57.          self.ftp.cwd( ".." )  

  58.          return

  59.         

  60.      def show( self, list  ):  

  61.          result = list.lower().split( " " )  

  62.          if self.path in result and "<dir>" in result:  

  63.              self.bIsDir = True

  64.          

  65.      def isDir( self, path ):  

  66.          self.bIsDir = False

  67.          self.path = path  

  68.          #this ues callback function ,that will change bIsDir value  

  69.          self.ftp.retrlines( 'LIST', self.show )  

  70.          return self.bIsDir  

  71.    

  72. ftp = KANFtp('xxxxx')  

  73. ftp.Login('xxxx','xxxxx')  

  74.    

  75. #ftp.DownLoadFile('TEST.TXT', 'public\\hechangmin\\TEST.TXT')#ok  

  76. #ftp.UpLoadFile('TEST.TXT', 'public\\hechangmin\\TEST.TXT')#ok  

  77. ftp.DownLoadFileTree('game999', 'game')#ok  

  78. # ftp.UpLoadFileTree('ts',"Public\\hechangmin\\testFTP" )  

  79. print "ok!"

复制代码
上面这段代码在网上看到的
DownLoadFileTree 这个函数试验一下有些问题  就是加入我的ftp家目录是test  我要下在test下的game目录 但game目录如果含有一个子目录 就会报错 下载不了  如果没有子目录就能下载成功

刚接触python没多久 各位大牛指点指点  
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP