09.11.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 17 ■ FTP<br />

these last few commands that FTP can be used to back file-browser applications that let users drag and<br />

drop files and directories seamlessly between their local system and the remote host.<br />

Doing FTP Securely<br />

Though we noted at the beginning <strong>of</strong> this chapter that there are far better protocols to adopt than FTP<br />

for pretty much anything you could use FTP to accomplish—in particular the robust and secure SFTP<br />

extension to SSH (see Chapter 16)—we should be fair and note that some few FTP servers support TLS<br />

encryption (see Chapter 6) and that <strong>Python</strong>’s ftplib does provide this protection if you want to take<br />

advantage <strong>of</strong> it.<br />

To use TLS, create your FTP connection with the FTP_TLS class instead <strong>of</strong> the plain FTP class; simply<br />

by doing this, your username and password and, in fact, the entire FTP command channel will be<br />

protected from prying eyes. If you then additionally run the class’s prot_p() method (it takes no<br />

arguments), then the FTP data connection will be protected as well. Should you for some reason want to<br />

return to using an un-encrypted data connection during the session, there is a prot_c() method that<br />

returns the data stream to normal. Again, your commands will continue to be protected as long as you<br />

are using the FTP_TLS class.<br />

Check the <strong>Python</strong> Standard Library documentation for more details (they include a small code<br />

sample) if you wind up needing this extension to FTP:<br />

http://docs.python.org/library/ftplib.html#ftplib.FTP_TLS<br />

Summary<br />

FTP lets you transfer files between a client running on your machine and a remote FTP server. Though<br />

the protocol is insecure and outdated when compared to better choices like SFTP, you might still find<br />

services and machines that require you to use it. In <strong>Python</strong>, the ftplib library is used to talk to FTP<br />

servers.<br />

FTP supports binary and ASCII transfers. ASCII transfers are usually used for text files, and permit<br />

line endings to be adjusted as the file is transferred. Binary transfers are used for everything else. The<br />

retrlines() function is used to download a file in ASCII mode, while retrbinary() downloads a file in<br />

binary mode.<br />

You can also upload files to a remote server. The storlines() function uploads a file in ASCII mode,<br />

and storbinary() uploads a file in binary mode.<br />

The ntransfercmd() function can be used for binary uploads and downloads. It gives you more<br />

control over the transfer process and is <strong>of</strong>ten used to support a progress bar for the user.<br />

The ftplib module raises exceptions on errors. The special tuple ftplib.all_errors can be used to<br />

catch any error that it might raise.<br />

You can use cwd() to change to a particular directory on the remote end. The nlst() command<br />

returns a simple list <strong>of</strong> all entries (files or directories) in a given directory. The dir() command returns a<br />

more detailed list but in server-specific format. Even with only nlst(), you can usually detect whether an<br />

entry is a file or directory by attempting to use cwd() to change to it and noting whether you get an error.<br />

303

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

Saved successfully!

Ooh no, something went wrong!