-
-
Save eadmaster/55d1508bd17780ee1e93c15609bb69af to your computer and use it in GitHub Desktop.
Recursively fetch files from an FTP server directory. Here, it's downloading all the zip files found in or beneath the parent directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# code derived from https://gist.githubusercontent.com/flibbertigibbet/8165881/raw/7f580b6735ad1f03c6e6060cd1c8571048afad6a/recurse_ftp.py | |
from ftplib import FTP | |
from time import sleep | |
import os | |
import sys | |
my_dirs = [] # global | |
curdir = '' # global | |
PROGRAM_NAME = os.path.basename(sys.argv[0]) | |
if len(sys.argv) <= 1 or sys.argv[1].startswith("-"): | |
print("usage: " + PROGRAM_NAME + " SERVER_ADDR [SERVER_ROOT_DIR]") | |
sys.exit(1) | |
# else | |
SERVER_ADDR = sys.argv[1] | |
SERVER_ROOT_PATH = "/" | |
if len(sys.argv) >= 3: | |
SERVER_ROOT_PATH = sys.argv[2] | |
def get_dirs(ln): | |
global my_dirs | |
cols = ln.split(' ') | |
objname = cols[len(cols)-1] # file or directory name | |
if ln.startswith('d'): | |
my_dirs.append(objname) | |
else: | |
print(curdir + "/" + objname) | |
def check_dir(adir): | |
global my_dirs | |
global curdir | |
my_dirs = [] | |
gotdirs = [] # local | |
try: | |
curdir = ftp.pwd() | |
sys.stderr.write(PROGRAM_NAME + ": going to change to directory " + adir + " from " + curdir + "\n") | |
ftp.cwd(adir) | |
curdir = ftp.pwd() | |
ftp.retrlines('LIST', get_dirs) | |
except: | |
return | |
print(curdir + "/") | |
gotdirs = my_dirs | |
#print("found in " + adir + " directories:") | |
sleep(0.5) | |
for subdir in gotdirs: | |
if(subdir.startswith(".")): | |
continue | |
my_dirs = [] | |
check_dir(subdir) # recurse | |
# end for | |
ftp.cwd('..') # back up a directory when done here | |
# end of check_dir | |
# main | |
try: | |
ftp = FTP(SERVER_ADDR) | |
ftp.login() | |
check_dir(SERVER_ROOT_PATH) # directory to start in | |
except: | |
import logging | |
logging.exception("") | |
ftp.quit() | |
ftp.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment