09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 17 ■ FTP<br />

» try:<br />

» » f.cwd(dirpath)<br />

» except error_perm:<br />

» » return # ignore non-directories and ones we cannot enter<br />

» print dirpath<br />

» names = f.nlst()<br />

» for name in names:<br />

» » walk_dir(f, dirpath + '/' + name)<br />

» f.cwd(original_dir) # return to cwd <strong>of</strong> our caller<br />

f = FTP('ftp.kernel.org')<br />

f.login()<br />

walk_dir(f, '/pub/linux/kernel/Historic/old-versions')<br />

f.quit()<br />

This sample program will run a bit slow—there are, it turns out, quite a few files in the old-versions<br />

directory on the Linux Kernel Archive—but within a few dozen seconds, you should see the resulting<br />

directory tree displayed on the screen:<br />

$ python recursedl.py<br />

/pub/linux/kernel/Historic/old-versions<br />

/pub/linux/kernel/Historic/old-versions/impure<br />

/pub/linux/kernel/Historic/old-versions/old<br />

/pub/linux/kernel/Historic/old-versions/old/corrupt<br />

/pub/linux/kernel/Historic/old-versions/tytso<br />

By adding a few print statements, you could supplement this list <strong>of</strong> directories by displaying every<br />

one <strong>of</strong> the files that the recursive process is (slowly) discovering. And by adding another few lines <strong>of</strong><br />

code, you could be downloading the files themselves to corresponding directories that you create locally.<br />

But the only really essential logic for a recursive download is already operating in Listing 17–9: the only<br />

foolpro<strong>of</strong> way to know if an entry is a directory that you are allowed to enter is to try running cwd()<br />

against it.<br />

Creating Directories, Deleting Things<br />

Finally, FTP supports file deletion, and supports both the creation and deletion <strong>of</strong> directories. These<br />

more obscure calls are all described in the ftplib documentation:<br />

• delete(filename) will delete a file from the server.<br />

• mkd(dirname) attempts to create a new directory.<br />

• rmd(dirname) will delete a directory; note that most systems require the directory<br />

to be empty first.<br />

• rename(oldname, newname) works, essentially, like the Unix command mv: if both<br />

names are in the same directory, the file is essentially re-named; but if the<br />

destination specifies a name in a different directory, then the file is actually<br />

moved.<br />

Note that these commands, like all other FTP operations, are performed more or less as though you<br />

were really logged on to the remote server command line as the same username with which you logged<br />

into FTP—and the chances <strong>of</strong> your having permission to manipulate files on a given server are lower<br />

than being able to download files, or even to create new files in an upload directory. Still, it is because <strong>of</strong><br />

302

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!