...lines omitted...
downloading ora-pyref4e.gif to test\ora-pyref4e.gif as image
downloading ora-lp4e-big.jpg to test\ora-lp4e-big.jpg as image
downloading ora-lp4e.gif to test\ora-lp4e.gif as image
downloading pyref4e-updates.html to test\pyref4e-updates.html as text
downloading lp4e-updates.html to test\lp4e-updates.html as text
downloading lp4e-examples.html to test\lp4e-examples.html as text
downloading LP4E-examples.zip to test\LP4E-examples.zip as application
Done: 297 files downloaded.
This may take a few moments to complete, depending on your site’s size and your
connection speed (it’s bound by network speed constraints, and it usually takes roughly
two to three minutes for my site on my current laptop and wireless broadband con-
nection). It is much more accurate and easier than downloading files by hand, though.
The script simply iterates over all the remote files returned by the nlst method, and
downloads each with the FTP protocol (i.e., over sockets) in turn. It uses text transfer
mode for names that imply text data, and binary mode for others.
With the script running this way, I make sure the initial assignments in it reflect the
machines involved, and then run the script from the local directory where I want
the site copy to be stored. Because the target download directory is often not where the
script lives, I may need to give Python the full path to the script file. When run on a
server in a Telnet or SSH session window, for instance, the execution and script direc-
tory paths are different, but the script works the same way.
If you elect to delete local files in the download directory, you may also see a batch of
“deleting local...” messages scroll by on the screen before any “downloading...” lines
appear: this automatically cleans out any garbage lingering from a prior download. And
if you botch the input of the remote site password, a Python exception is raised; I
sometimes need to run it again (and type more slowly):
C:\...\PP4E\Internet\Ftp\Mirror> downloadflat.py test
Password for lutz on home.rmi.net:
Clean local directory first?
connecting...
Traceback (most recent call last):
File "C:\...\PP4E\Internet\Ftp\Mirror\downloadflat.py", line 29, in <module>
connection.login(remoteuser, remotepass) # login as user/password
File "C:\Python31\lib\ftplib.py", line 375, in login
if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
File "C:\Python31\lib\ftplib.py", line 245, in sendcmd
return self.getresp()
File "C:\Python31\lib\ftplib.py", line 220, in getresp
raise error_perm(resp)
ftplib.error_perm: 530 Login incorrect.
It’s worth noting that this script is at least partially configured by assignments near the
top of the file. In addition, the password and deletion options are given by interactive
inputs, and one command-line argument is allowed—the local directory name to store
Transferring Directories with ftplib | 879