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 />

f.login()<br />

f.cwd('/pub/academic/astronomy/')<br />

entries = []<br />

f.dir(entries.append)<br />

print "%d entries:" % len(entries)<br />

for entry in entries:<br />

» print entry<br />

f.quit()<br />

Notice that the filenames are in a convenient format for automated processing — a bare list <strong>of</strong><br />

filenames — but that is no extra information. Contrast the bare list <strong>of</strong> file names we saw earlier with the<br />

output from Listing 17–8, which uses dir():<br />

$ python dir.py<br />

13 entries:<br />

-rw-r--r-- 1 (?) » (?) » » 750 Feb 14 1994 INDEX<br />

-rw-r--r-- 1 root » bin » » 135 Feb 11 1999 README<br />

-rw-r--r-- 1 (?) » (?) » 341303 Oct 2 1992 ephem_4.28.tar.Z<br />

drwxr-xr-x 2 (?) » (?) » » 4096 Feb 11 1999 hawaii_scope<br />

drwxr-xr-x 2 (?) » (?) » » 4096 Feb 11 1999 incoming<br />

-rw-r--r-- 1 (?) » (?) » » 5983 Oct 2 1992 jupitor-moons.shar.Z<br />

-rw-r--r-- 1 (?) » (?) » » 1751 Oct 2 1992 lunar.c.Z<br />

-rw-r--r-- 1 (?) » (?) » » 8078 Oct 2 1992 lunisolar.shar.Z<br />

-rw-r--r-- 1 (?) » (?) » » 64209 Oct 2 1992 moon.shar.Z<br />

drwxr-xr-x 2 (?) » (?) » » 4096 Jan 6 1993 planetary<br />

-rw-r--r-- 1 (?) » (?) » 129969 Oct 2 1992 sat-track.tar.Z<br />

-rw-r--r-- 1 (?) » (?) » » 16504 Oct 2 1992 stars.tar.Z<br />

-rw-r--r-- 1 (?) » (?) » 410650 Oct 2 1992 xephem.tar.Z<br />

The dir() method takes a function that it calls for each line, delivering the directory listing in pieces<br />

just like retrlines() delivers the contents <strong>of</strong> particular files. Here, we simply supply the append()<br />

method <strong>of</strong> our plain old <strong>Python</strong> entries list.<br />

Detecting Directories and Recursive Download<br />

If you cannot guarantee what information an FTP server might choose to return from its dir()<br />

command, how are you going to tell directories from normal files—an essential step to downloading<br />

entire trees <strong>of</strong> files from the server?<br />

The answer, shown in Listing 17–9, is to simply try a cwd() into every name that nlst() returns and,<br />

if you succeed, conclude that the entity is a directory! This sample program does not do any actual<br />

downloading; instead, to keep things simple (and not flood your disk with sample data), it simply prints<br />

out the directories it visits to the screen.<br />

Listing 17–9. Trying to Recurse into Directories<br />

#!/usr/bin/env python<br />

# Recursive downloader - Chapter 17 - recursedl.py<br />

import os, sys<br />

from ftplib import FTP, error_perm<br />

def walk_dir(f, dirpath):<br />

» original_dir = f.pwd()<br />

301

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

Saved successfully!

Ooh no, something went wrong!