the downloaded files (it defaults to “.”, the directory where the script is run).
Command-line arguments could be employed to universally configure all the other
download parameters and options, too, but because of Python’s simplicity and lack of
compile/link steps, changing settings in the text of Python scripts is usually just as easy
as typing words on a command line.
To check for version skew after a batch of downloads and uploads, you
can run the diffall script we wrote in Chapter 6, Example 6-12. For
instance, I find files that have diverged over time due to updates on
multiple platforms by comparing the download to a local copy of my
website using a shell command line such as C:\...\PP4E\Internet
\Ftp> ..\..\System\Filetools\diffall.py Mirror\test C:\...\Web
sites\public_html. See Chapter 6 for more details on this tool, and file
diffall.out.txt in the diffs subdirectory of the examples distribution for a
sample run; its text file differences stem from either final line newline
characters or newline differences reflecting binary transfers that Win-
dows fc commands and FTP servers do not notice.
Uploading Site Directories
Uploading a full directory is symmetric to downloading: it’s mostly a matter of swap-
ping the local and remote machines and operations in the program we just met. The
script in Example 13-11 uses FTP to copy all files in a directory on the local machine
on which it runs up to a directory on a remote machine.
I really use this script, too, most often to upload all of the files maintained on my laptop
PC to my ISP account in one fell swoop. I also sometimes use it to copy my site from
my PC to a mirror machine or from the mirror machine back to my ISP. Because this
script runs on any computer with Python and sockets, it happily transfers a directory
from any machine on the Net to any machine running an FTP server. Simply change
the initial setting in this module as appropriate for the transfer you have in mind.
Example 13-11. PP4E\Internet\Ftp\Mirror\uploadflat.py
#!/bin/env python
"""
##############################################################################
use FTP to upload all files from one local dir to a remote site/directory;
e.g., run me to copy a web/FTP site's files from your PC to your ISP;
assumes a flat directory upload: uploadall.py does nested directories.
see downloadflat.py comments for more notes: this script is symmetric.
##############################################################################
"""
import os, sys, ftplib
from getpass import getpass
from mimetypes import guess_type
880 | Chapter 13: Client-Side Scripting