FTP Download Script

A very basic python script that downloads a website’s contents (via FTP).

Versions:
1.0: Initial program

from ftplib import FTP
from datetime import date, datetime
import os.path

# Server IP
Server_Address = ""
# An array of bad files or directories 
BadFiles = []

#Username for Server
user = ""

#Password is below

def swap_crypt(str1):
    """
    this function will encrypt/decrypt a string
    """
    # change string to a mutable list of characters
    list1 = list(str1)
    # iterate the list with step=2
    for k in range(0, len(list1), 2):
        if len(list1) > k + 1:
            # do a swap of groups of 2 items each in the list
            list1[k], list1[k+1] = list1[k+1], list1[k]
    # change list back to a string
    return ''.join(list1)

def handleDownload(block):
    global f
    f.write(block)

def RecurseDirectories(currdir, folder, files):
    global f, BadFiles, backup_dir
    returnArr = []
    if currdir == "":
        filepath = ""
    else:
        filepath = currdir + "/"
    for x in folder:
        try:
            BadFiles.index(x)
            print "Skipped ", x
        except:
            try:
                if (ftp.size(filepath + x) >= 0):
                    print filepath + x + " ... ",
                    if not os.path.isdir(backup_dir + "/" + filepath):
                        os.makedirs(backup_dir + "/" +filepath)
                    f = open(backup_dir + "/" +filepath + x, "wb")
                    ftp.retrbinary('RETR ' + filepath + x, handleDownload)
                    f.close()
                    print "Complete"
                    returnArr.append(filepath + x)
            except:
                try:
                    print "Dir: " + filepath + x
                    # Remove . and .. from directory listings
                    directory = ftp.nlst(filepath + x)
                    try:
                        directory.pop(directory.index("."))
                    except:
                        #Do Nothing
                        f = ""
                    try:  
                        directory.pop(directory.index(".."))
                    except:
                        #Do Nothing
                        f = ""

                    for y in RecurseDirectories(filepath + x, directory, []):
                        returnArr.append(y)
                except:
                    # Do Nothing
                    f = ""
    return returnArr
f = ""

#Password for Server
# Swapped password: test
# print swap_crypt("test")  # Uncomment out to get your crypted password
password = swap_crypt("etts")

ftp = FTP(Server_Address)   # connect to host, default port
ftp.login(user, password)   # user anonymous, passwd anonymous@
ftp.set_pasv(True)

d = str(datetime.now())
day = d[0:str(d).index(" ")]

# Determine where we are going to throw this directory
backup_dir = "Backups_Website/" + day
# Make sure that the directory is unique and that we have not already written the directory
j = ""
i = 0
while os.path.isdir(backup_dir+j):
    i = i + 1
    j = "-" + str(i)
backup_dir = backup_dir+j

curr_dir = ""
directory_arr = backup_dir.split("/")
for x in directory_arr:
    if not os.path.isdir(curr_dir + x) and x != "":
        os.makedirs(curr_dir + x)
    curr_dir += x + "/"

#Remove the first two directories but walk the rest
directory = ftp.nlst("")[2:]

# Recursively move through the ftp structure and store the filepaths
filepaths = RecurseDirectories("", directory, [])

Posted

in

by

Tags: