#!/usr/bin/python
from ftplib import FTP
import netrc
import re
import sys
directory = {"siteid":["hostname", "root directory of your ftp site"], "another_site":["another host", "another root directory"]}
if len(sys.argv) > 2:
host = sys.argv[1]
off_file = 2
else:
host = "hostname"
off_file = 1
if not directory.has_key(host):
print host, "is not currently supported"
sys.exit(1)
# we use .netrc to store userid and password info.
net = netrc.netrc()
(user, acct, passwd) = net.authenticators(directory[host][0])
def main(argv):
print "connecting ...", directory[host][0]
ftp = FTP(directory[host][0])
print "login'ing ...", directory[host][0]
ftp.login(user, passwd)
for i in range(len(argv)):
sys.stdout.write("getting ... " + argv[i])
p = re.compile('(.*)(/[^/]+)')
m = p.match(argv[i])
if m:
path = m.group(1)
filename = m.group(2)[1:]
ftp.cwd(directory[host][1] + path)
if len(path):
file = open(argv[i], 'wb')
else:
file = open(filename, 'wb')
ftp.retrbinary("RETR " + filename, file.write)
print " done"
file.close()
else:
print "no matching .."
ftp.close()
if __name__ == '__main__':
if (len(sys.argv) > 1):
main(sys.argv[off_file:])
else:
print "Usage: get [host] file_to_be_get\nHost: siteid(default), another_site\n"