Created
December 14, 2012 18:30
-
-
Save miikka/4287506 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
import fnmatch | |
from ftplib import FTP | |
import os | |
import sys | |
class Downloader(object): | |
destdir = '.' | |
pattern = '*' | |
def __init__(self, host, user, pw, delete=False): | |
self.ftp = FTP(host, user, pw) | |
self.delete = delete | |
def get(self, path): | |
parts = path.split('/') | |
last = parts.pop() | |
for part in parts: | |
self.ftp.cwd(part) | |
self.handle_directory(last) | |
def download(self, filename): | |
with open(os.path.join(self.destdir, filename), 'wb') as handle: | |
self.ftp.retrbinary('RETR ' + filename, handle.write) | |
def handle_directory(self, path): | |
self.ftp.cwd(path) | |
queue = [] | |
def handle_line(line): | |
parts = line.split() | |
queue.append(parts) | |
self.ftp.retrlines("LIST", handle_line) | |
for item in queue: | |
filename = item[3] | |
filesize = item[2] | |
print filename | |
if filesize == '<DIR>': | |
self.handle_directory(filename) | |
elif fnmatch.fnmatch(filename, self.pattern): | |
self.download(filename) | |
assert int(filesize) == os.path.getsize(os.path.join(self.destdir, filename)) | |
if self.delete: | |
self.ftp.delete(filename) | |
self.ftp.cwd('..') | |
if __name__ == "__main__": | |
import argparse | |
parser = argparse.ArgumentParser(description='Download a directory structure over FTP.') | |
parser.add_argument('host', help='hostname of the FTP server') | |
parser.add_argument('path', help='path to download') | |
parser.add_argument('--user', help='FTP user name') | |
parser.add_argument('--password', help='FTP password') | |
parser.add_argument('--pattern', help='pattern of files to download (default: *)', | |
default='*') | |
parser.add_argument('--destdir', help='destination directory (default: current directory)', | |
default='.') | |
parser.add_argument('--delete', action='store_const', const=True, default=False, | |
help='delete files after downloading them') | |
args = parser.parse_args() | |
if args.delete: | |
print "DELETE MODE ENGAGED" | |
if not os.path.exists(args.destdir): | |
print "Destination directory %s does not exist." % args.destdir | |
sys.exit(1) | |
dl = Downloader(args.host, args.user, args.password, args.delete) | |
dl.destdir = args.destdir | |
dl.pattern = args.pattern | |
dl.get(args.path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment