[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

In the next chapter, we’ll look at a complete client-side program example before moving
on to explore scripts designed to be run on the server side instead. Python programs
can also produce pages on a web server, and there is support in the Python world for
implementing the server side of things like HTTP, email, and FTP. For now, let’s focus
on the client.*


FTP: Transferring Files over the Net


As we saw in the preceding chapter, sockets see plenty of action on the Net. For in-
stance, the last chapter’s 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 proto-
cols. 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, standard and more
general—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 op-
erations 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.


Transferring Files with ftplib


Because the Python FTP interface is so easy to use, let’s jump right into a realistic
example. The script in Example 13-1 automatically fetches (a.k.a. “downloads”) and



  • There is also support in the Python world for other technologies that some might classify as “client-side
    scripting,” too, such as Jython/Java applets; XML-RPC and SOAP web services; and Rich Internet Application
    tools like Flex, Silverlight, pyjamas, and AJAX. These were all introduced early in Chapter 12. Such tools are
    generally bound up with the notion of web-based interactions—they either extend the functionality of a web
    browser running on a client machine, or simplify web server access in clients. We’ll study browser-based
    techniques in Chapters 15 and 16; here, client-side scripting means the client side of common Internet
    protocols such as FTP and email, independent of the Web or web browsers. At the bottom, web browsers
    are really just desktop GUI applications that make use of client-side protocols, including those we’ll study
    here, such as HTTP and FTP. See Chapter 12 as well as the end of this chapter for more on other client-side
    techniques.


854 | Chapter 13: Client-Side Scripting

Free download pdf