File: class/Workbook/Examples/Lecture14/getone.py
import os, sys from getpass import getpass nonpassive = False filename = 'lawnlake2-jan-03.jpg' # file to download dirname = '.' # remote directory sitename = 'ftp.rmi.net' # ftp site to contact userinfo = ('lutz', getpass('Pswd?')) # use () for anonymous if len(sys.argv) > 1: filename = sys.argv[1] # file on command-line? print 'Connecting...' from ftplib import FTP # socket-based ftp tools localfile = open(filename, 'wb') # local file to store to connection = FTP(sitename) # connect to ftp site connection.login(*userinfo) # default anonymous login connection.cwd(dirname) # xfer 1k at a time if nonpassive: # if server requires connection.set_pasv(False) # thread me in a GUI print 'Downloading...' connection.retrbinary('RETR ' + filename, localfile.write, 1024) connection.quit() localfile.close()