#!/usr/bin/python # Copyright 1998-2007 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 import os, sys BUFSIZE = 8192 if __name__ == "__main__": from optparse import OptionParser usage = "%s [OPTION]... [URL]..." % os.path.basename(sys.argv[0]) parser = OptionParser(usage=usage) parser.add_option("-O", "--output-document", dest="filename", help="save uri to FILE", metavar="FILE") parser.add_option("-c", "--continue", action="store_true", dest="cont", help="continue getting a partially-downloaded file") parser.add_option("-b", "--blocksize", type="int", help="block size used for read/write", default=BUFSIZE) parser.add_option("-q", "--quiet", action="store_true", help="disable output") options, args = parser.parse_args(sys.argv[1:]) if len(args) != 1: parser.error("missing URL") uri = args[0] parts = uri.split("://", 1) if len(parts) != 2: parser.error("invalid URL: % '%s'" % uri) protocol = parts[0] parts = parts[1].split("/", 1) if len(parts) != 2: parser.error("invalid URL: % '%s'" % uri) host = parts[0] path = parts[1] userpass_host = host.split("@", 1) if len(userpass_host) != 2: parser.error("invalid host name: '%s'" % host) else: host = userpass_host[1] userpass = userpass_host[0].split(":") if len(userpass) > 2: parser.error("invalid user/password: % '%s'" % userpass) elif len(userpass) == 2: username = userpass[0] password = userpass[1] elif len(userpass) == 1: username = userpass[0] password = None filename = options.filename if not filename: filename = os.path.basename(uri) size = 0 if options.cont: try: size = os.stat(filename).st_size except EnvironmentError: pass bufsize = options.blocksize quiet = options.quiet stdout = sys.stdout if size: dest = open(filename, "a") else: dest = open(filename, "w") import paramiko t = paramiko.Transport(host) t.connect(username=username, password=password) conn = paramiko.SFTPClient.from_transport(t) f = conn.open(path) if size: f.seek(size) try: while True: data = f.read(bufsize) if not data: break dest.write(data) if not quiet: stdout.write(".") stdout.flush() finally: f.close() if not quiet: stdout.write("\n") stdout.flush()