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

(yzsuai) #1
Python scripts can connect to all of these Internet components by simply importing the
associated library module.
Since Grail, additional tools have been added to Python’s library for parsing XML files,
OpenSSL secure sockets, and more. But much of Python’s Internet support can be
traced back to the Grail browser—another example of Python’s support for code reuse
at work. At this writing, you can still find the Grail by searching for “Grail web browser”
at your favorite web search engine.

Python Internet Development Options


Although many are outside our scope here, there are a variety of ways that Python
programmers script the Web. Just as we did for GUIs, I want to begin with a quick
overview of some of the more popular tools in this domain before we jump into the
fundamentals.


Networking tools
As we’ll see in this chapter, Python comes with tools the support basic networking,
as well as implementation of custom types of network servers. This includes sock-
ets, but also the select call for asynchronous servers, as well as higher-order and
pre-coded socket server classes. Standard library modules socket, select, and
socketserver support all these roles.


Client-side protocol tools
As we’ll see in the next chapter, Python’s Internet arsenal also includes canned
support for the client side of most standard Internet protocols—scripts can easily
make use of FTP, email, HTTP, Telnet, and more. Especially when wedded to
desktop GUIs of the sort we met in the preceding part of this book, these tools
open the door to full-featured and highly responsive Web-aware applications.


Server-side CGI scripting
Perhaps the simplest way to implement interactive website behavior, CGI script-
ing is an application model for running scripts on servers to process form data, take
action based upon it, and produce reply pages. We’ll use it later in this part of the
book. It’s supported by Python’s standard library directly, is the basis for much of
what happens on the Web, and suffices for simpler site development tasks. Raw
CGI scripting doesn’t by itself address issues such as cross-page state retention and
concurrent updates, but CGI scripts that use devices like cookies and database
systems often can.


Web frameworks and clouds
For more demanding Web work, frameworks can automate many of the low-level
details and provide more structured and powerful techniques for dynamic site im-
plementation. Beyond basic CGI scripts, the Python world is flush with third-party
web frameworks such as Django—a high-level framework that encourages rapid
development and clean, pragmatic design and includes a dynamic database access


Python Internet Development Options | 777
Free download pdf