Transferring Directories with ftplib
Once upon a time, I used Telnet to manage my website at my Internet Service Provider
(ISP). I logged in to the web server in a shell window, and performed all my edits directly
on the remote machine. There was only one copy of a site’s files—on the machine that
hosted it. Moreover, content updates could be performed from any machine that ran
a Telnet client—ideal for people with travel-based careers.†
Of course, times have changed. Like most personal websites, today mine are maintained
on my laptop and I transfer their files to and from my ISP as needed. Often, this is a
simple matter of one or two files, and it can be accomplished with a command-line FTP
client. Sometimes, though, I need an easy way to transfer the entire site. Maybe I need
to download to detect files that have become out of sync. Occasionally, the changes
are so involved that it’s easier to upload the entire site in a single step.
Although there are a variety of ways to approach this task (including options in site-
builder tools), Python can help here, too: writing Python scripts to automate the upload
and download tasks associated with maintaining my website on my laptop provides a
portable and mobile solution. Because Python FTP scripts will work on any machine
with sockets, they can be run on my laptop and on nearly any other computer where
Python is installed. Furthermore, the same scripts used to transfer page files to and
from my PC can be used to copy my site to another web server as a backup copy, should
my ISP experience an outage. The effect is sometimes called a mirror—a copy of a
remote site.
Downloading Site Directories
The following two scripts address these needs. The first, downloadflat.py, automatically
downloads (i.e., copies) by FTP all the files in a directory at a remote site to a directory
on the local machine. I keep the main copy of my website files on my PC these days,
but I use this script in two ways:
- To download my website to client machines where I want to make edits, I fetch
the contents of my web directory of my account on my ISP’s machine. - To mirror my site to my account on another server, I run this script periodically
on the target machine if it supports Telnet or SSH secure shell; if it does not, I
simply download to one machine and upload from there to the target server.
† No, really. The second edition of this book included a tale of woe here about how my ISP forced its users to
wean themselves off Telnet access. This seems like a small issue today. Common practice on the Internet has
come far in a short time. One of my sites has even grown too complex for manual edits (except, of course,
to work around bugs in the site-builder tool). Come to think of it, so has Python’s presence on the Web.
When I first found Python in 1992, it was a set of encoded email messages, which users decoded and
concatenated and hoped the result worked. Yes, yes, I know—gee, Grandpa, tell us more...
874 | Chapter 13: Client-Side Scripting