Chapter 17 ■ Ftp
330
• rename(oldname, newname) works, essentially, like the Unix command mv: if both names are
in the same directory, the file is essentially renamed; but if the destination specifies a name in
a different directory, then the file is actually moved.
Note that these commands, like all other FTP operations, are performed more or less as though you were really
logged on to the remote server command line with the same username with which you logged in to the FTP. It is
because of these last few commands that FTP can be used to back file browser applications that let users drag and
drop files and directories seamlessly between their local system and the remote host.
Doing FTP Securely
Though I noted at the beginning of this chapter that there are far better protocols to adopt than FTP for pretty much
anything you could use FTP to accomplish, in particular the robust and secure SFTP extension to SSH (see Chapter 16),
I should be fair and note that a few FTP servers support TLS encryption (see Chapter 6) and that Python’s ftplib does
provide this protection if you want to take advantage of it.
To use TLS, create your FTP connection with the FTP_TLS class instead of the plain FTP class. Simply by doing
this, your username and password and, in fact, the entire FTP command channel will be protected from prying eyes.
If you then additionally run the class’s prot_p() method (it takes no arguments), then the FTP data connection will
be protected as well. Should you, for some reason, want to return to using an unencrypted data connection during the
session, there is a prot_c() method that returns the data stream to normal. Again, your commands will continue to be
protected as long as you are using the FTP_TLS class.
Check the Python Standard Library documentation for more details (they include a small code sample) if you
wind up needing this extension to FTP: http://docs.python.org/3/library/ftplib.html.
Summary
FTP lets you transfer files between a client running on your machine and a remote FTP server. Though the protocol is
not secure and is outdated when compared with better choices like SFTP, you might still find services and machines
that require you to use it. In Python, the ftplib library is used to talk to FTP servers.
FTP supports binary and ASCII transfers. ASCII transfers are usually used for text files, and they permit line
endings to be adjusted as the file is transferred. Binary transfers are used for everything else. The retrlines()
function is used to download a file in ASCII mode, while retrbinary() downloads a file in binary mode.
You can also upload files to a remote server. The storlines() function uploads a file in ASCII mode, and
storbinary() uploads a file in binary mode.
The ntransfercmd() function can be used for binary uploads and downloads. It gives you more control over the
transfer process, and it is often used to support a progress bar for the user.
The ftplib module raises exceptions on errors. The special tuple ftplib.all_errors can be used to catch any
error that it might raise.
You can use cwd() to change to a particular directory on the remote end. The nlst() command returns a
simple list of all entries (files or directories) in a given directory. The dir() command returns a more detailed list,
but in server-specific format. Even with only nlst(), you can usually detect whether an entry is a file or directory by
attempting to use cwd() to change to it and noting whether you get an error.
In the next chapter, we turn from the simple action of file transfer to the more generic action of invoking a remote
procedure on another server and getting typed data, instead of bare strings, back in response.