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

(yzsuai) #1
strings of information that are stored on the client upon request from the server,
and that are transferred back to the server when a page is revisited (data is sent
back and forth in HTTP header lines). Cookies are more complex than program
variables and are somewhat controversial and optional, but they can offload some
simple state retention tasks.
Alternative models such as FastCGI and mod_python offer additional persistence
options—where supported, FastCGI applications may retain context in long-lived
processes, and mod_python provides session data within Apache.

HTML generation solutions
Third-party extensions can also take some of the complexity out of embedding
HTML in Python CGI scripts, albeit at some cost to execution speed. For instance,
the HTMLgen system and its relatives let programs build pages as trees of Python
objects that “know” how to produce HTML. Other frameworks prove an object-
based interface to reply-stream generation (e.g., a reply object with methods).
When a system like this is employed, Python scripts deal only with objects, not
with the syntax of HTML itself.
For instance, systems such as PHP, Python Server Pages (PSP), Zope’s DTML and
ZPT, and Active Server Pages provide server-side templating languages, which al-
low scripting language code to be embedded in HTML and executed on the server,
to dynamically generate or determine part of the HTML that is sent back to a client
in response to requests. The net result can cleanly insulate Python code from the
complexity of HTML code and promote the separation of display format and busi-
ness logic, but may add complexities of its own due to the mixture of different
languages.


Generalized user interface development
To cover both bases, some systems attempt to separate logic from display so much
as to make the choice almost irrelevant—by completely encapsulating display de-
tails, a single program can, in principle, render its user interface as either a tradi-
tional GUI or an HTML-based web page. Due to the vastly different architectures,
though, this ideal is difficult to achieve well and does not address larger disparities
between the client and server platforms. Issues such as state retention and network
interfaces are much more significant than generation of windows and controls, and
may impact code more.
Other systems may try to achieve similar goals by abstracting the display repre-
sentation—a common XML representation, for instance, might lend itself to both
a GUI and an HTML rendering. Again, though, this addresses only the rendering
of the display, not the fundamental architectural differences of client- and server-
side approaches.


Emerging technologies: RIAs and HTML 5
Finally, higher-level approaches such as the RIA (Rich Internet Application) tool-
kits introduced in Chapters 7 and 12 can offer additional functionality that HTML
lacks and can approach the utility on GUI toolkits. On the other hand, they can


Web Scripting Trade-Offs| 1297
Free download pdf