Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active August 29, 2015 14:01
Show Gist options
  • Save lidio601/5d1790d66363fdf738a1 to your computer and use it in GitHub Desktop.
Save lidio601/5d1790d66363fdf738a1 to your computer and use it in GitHub Desktop.
Download a folder via FTP. In a python shell run scarica_cartella("IPADDRESS","USERNAME","PASSWORD","REMOTE-FOLDER","DESTINATION-FOLDER")
import ftplib
import os
def scarica_cartella(host,user,password,cartella_richiesta,destinazione="./"):
try:
ftpp=ftplib.FTP(host,user,password)
except:
print "connessione non riuscita"
ftpp.close()
else:
#controllo se la cartella di destinazione esiste
if os.path.exists(destinazione)==False:
try:
os.system("mkdir -p '%s'" % (destinazione))
except:
print "Non hai i permessi per scrivere qui <%s>!" %(destinazione)
#else:
#print "Cartella di destinazione creata"
filetemp=".comando.lftp.tmp"
comando="open -u %s,%s %s\nmirror --no-perms --no-umask --parallel=2 -v --continue %s %s"%(user,password,host,cartella_richiesta,destinazione)
os.system("echo '%s' >%s"%(comando,filetemp))
try:
os.system("lftp -f '%s'" % (filetemp))
except:
print "non sono riuscito a eseguire il comando esterno <lftp>!!!"
ftpp.close()
else:
os.system("rm %s"%(filetemp))
ftpp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment