Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Python FTP Dump

By: a guest on Mar 6th, 2013  |  syntax: Python  |  size: 1.58 KB  |  views: 219  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import ftplib, os, shutil, re
  2.  
  3. host = "ftp.server.com"
  4. port = 21
  5. user = "username"
  6. password = "password"
  7. user = ""
  8. password = ""
  9.  
  10. ftp = ftplib.FTP()
  11. ftp.connect(host,port)
  12. ftp.login(user,password)
  13.  
  14. def output(out):
  15.         return 0
  16.  
  17. def copy_directory(directory="", depth=0,exceptions = None):
  18.         if exceptions != None and len(exceptions) > 0:
  19.                 re_str = ("(" + "|".join(exceptions) + ")").lower()
  20.         else:
  21.                 re_str = "^$"
  22.         if exceptions == None:
  23.                 exceptions = []
  24.         elif re.match(re_str,directory.lower()):
  25.                 return
  26.         newdir = os.path.join(os.getcwd(),host)
  27.         if(directory != ""):
  28.                 newdir = os.path.join(os.getcwd(),directory)
  29.                
  30.         if(os.path.exists(newdir)):
  31.                 shutil.rmtree(newdir)
  32.         os.mkdir(newdir)
  33.         os.chdir(newdir)
  34.         files = []
  35.         try:
  36.                 ftp.cwd(directory)
  37.         except ftplib.error_perm:
  38.                 return
  39.         try:
  40.                         files = ftp.nlst()
  41.         except ftplib.error_perm:
  42.                         print((" "+("  |" * (depth))+"-No Files in Folder"))
  43.         print(" "+("  |" * (depth)))
  44.         if(len(files) == 0):
  45.                 print(" "+("  |" * (depth))+"-No Files in Folder")
  46.         for f in files:
  47.                 if(f == "." or f == ".."):
  48.                                 continue
  49.                 print(" "+("  |" * (depth))+"-"+f,"- (IGNORED)" if re.match(re_str,f.lower()) else "")
  50.                 if re.match(re_str,f.lower()):
  51.                         continue
  52.                 try:
  53.                         ftp.size(f)
  54.                         local_f = os.path.join(os.getcwd(),f)
  55.                         fh = open(local_f, "wb")
  56.                         ftp.retrbinary("RETR "+f, fh.write, 8*1024)
  57.                         fh.close()
  58.                 except ftplib.error_perm as err:
  59.                         if(err.args[0].count("553") > 0):
  60.                                 continue
  61.                         copy_directory(f,depth+1,exceptions)
  62.         print(" "+("  |" * (depth-1)))
  63.         ftp.cwd("..")
  64.         os.chdir("..")
  65.  
  66. copy_directory()