The ftplib Module

The ftplib module contains a File Transfer Protocol (FTP) client implementation.

Example 7-22 demonstrates how to log in and get a directory listing of the login directory. Note that the format of the directory listing is server dependent (it's usually the same as the format used by the directory listing utility on the server host platform).

Example 7-22. Using the ftplib Module to Get a Directory Listing

File: ftplib-example-1.py import ftplib ftp = ftplib.FTP("www.python.org") ftp.login("anonymous", "ftplib-example-1") print ftp.dir() ftp.quit() total 34 drwxrwxr-x 11 root 4127 512 Sep 14 14:18 . drwxrwxr-x 11 root 4127 512 Sep 14 14:18 .. drwxrwxr-x 2 root 4127 512 Sep 13 15:18 RCS lrwxrwxrwx 1 root bin 11 Jun 29 14:34 README -> welcome.msg drwxr-xr-x 3 root wheel 512 May 19 1998 bin drwxr-sr-x 3 root 1400 512 Jun 9 1997 dev drwxrwxr-- 2 root 4127 512 Feb 8 1998 dup drwxr-xr-x 3 root wheel 512 May 19 1998 etc ...

Downloading files is easy; just use the appropriate retr function. Note that when you download a text file, you have to add line endings yourself. The function in Example 7-23 uses a lambda expression to do that on the fly.

Example 7-23. Using the ftplib Module to Retrieve Files

File: ftplib-example-2.py import ftplib import sys def gettext(ftp, filename, outfile=None): # fetch a text file if outfile is None: outfile = sys.stdout # use a lambda to add newlines to the lines read from the server ftp.retrlines("RETR " + filename, lambda s, w=outfile.write: w(s+" ")) def getbinary(ftp, filename, outfile=None): # fetch a binary file if outfile is None: outfile = sys.stdout ftp.retrbinary("RETR " + filename, outfile.write) ftp = ftplib.FTP("www.python.org") ftp.login("anonymous", "ftplib-example-2") gettext(ftp, "README") getbinary(ftp, "welcome.msg") WELCOME to python.org, the Python programming language home site. You are number %N of %M allowed users. Ni! Python Web site: http://www.python.org/ CONFUSED FTP CLIENT? Try begining your login password with '-' dash. This turns off continuation messages that may be confusing your client. ...

Finally, Example 7-24 is a simple one that copies files to the FTP server. This script uses the file extension to figure out if the file is a text file or a binary file.

Example 7-24. Using the ftplib Module to Store Files

File: ftplib-example-3.py import ftplib import os def upload(ftp, file): ext = os.path.splitext(file)[1] if ext in (".txt", ".htm", ".html"): ftp.storlines("STOR " + file, open(file)) else: ftp.storbinary("STOR " + file, open(file, "rb"), 1024) ftp = ftplib.FTP("ftp.fbi.gov") ftp.login("mulder", "trustno1") upload(ftp, "trixie.zip") upload(ftp, "file.txt") upload(ftp, "sightings.jpg")

Категории