As we saw in the preceding chapter, sockets see plenty
of action on the Net. For instance, the
getfile example allowed us to transfer
entire files between machines. In practice, though, higher-level
protocols are behind much of what happens on the Net. Protocols run on
top of sockets, but they hide much of the complexity of the network
scripting examples of the prior chapter.
FTP—the File Transfer Protocol—is one of the more commonly used
Internet protocols. It defines a higher-level conversation model that
is based on exchanging command strings and file contents over sockets.
By using FTP, we can accomplish the same task as the prior chapter’s
getfile script, but the interface
is simpler and standard—FTP lets us ask for files from any server
machine that supports FTP, without requiring that it run our custom
getfile script. FTP also supports
more advanced operations such as uploading files to the server,
getting remote directory listings, and more.
Really, FTP runs on top of two sockets: one for passing control
commands between client and server (port 21), and another for
transferring bytes. By using a two-socket model, FTP avoids the
possibility of deadlocks (i.e., transfers on the data socket do not
block dialogs on the control socket). Ultimately, though, Python’s
ftplib support module allows us to
upload and download files at a remote server machine by FTP, without
dealing in raw socket calls or FTP protocol details.