I l@ve RuBoard |
15.9 Python Server PagesThough still somewhat new at this writing, Python Server Pages (PSP) is a server-side technology that embeds JPython code inside HTML. PSP is a Python-based answer to other server-side embedded scripting approaches. The PSP scripting engine works much like Microsoft's Active Server Pages (ASP, described earlier) and Sun's Java Server Pages ( JSP) specification. At the risk of pushing the acronym tolerance envelope, PSP has also been compared to PHP, a server-side scripting language embedded in HTML. All of these systems, including PSP, embed scripts in HTML and run them on the server to generate the response stream sent back to the browser on the client; scripts interact with an exposed object model API to get their work done. PSP is written in pure Java, however, and so is portable to a wide variety of platforms (ASP applications can be run only on Microsoft platforms). PSP uses JPython as its scripting language, reportedly a vastly more appropriate choice for scripting web sites than the Java language used in Java Server Pages. Since JPython code is embedded under PSP, scripts have access to the large number of Python and JPython tools and add-ons from within PSPs. In addition, scripts may access all Java libraries, thanks to JPython's Java integration support. We can't cover PSP in detail here; but for a quick look, Example 15-18, adapted from an example in the PSP documentation, illustrates the structure of PSPs. Example 15-18. PP2E\Internet\Other\hello.psp$[ # Generate a simple message page with the client's IP address ]$ <HTML><HEAD> <TITLE>Hello PSP World</TITLE> </HEAD> <BODY> $[include banner.psp]$ <H1>Hello PSP World</H1> <BR> $[ Response.write("Hello from PSP, %s." % (Request.server["REMOTE_ADDR"]) ) ]$ <BR> </BODY></HTML> A page like this would be installed on a PSP-aware server machine and referenced by URL from a browser. PSP uses $[ and ]$ delimiters to enclose JPython code embedded in HTML; anything outside these pairs is simply sent to the client browser, while code within these markers is executed. The first code block here is a JPython comment (note the # character); the second is an include statement that simply inserts another PSP file's contents. The third piece of embedded code is more useful. As in Active Scripting technologies, Python code embedded in HTML uses an exposed object API to interact with the execution context -- in this case, the Response object is used to write output to the client's browser (much like a print in a CGI script), and Request is used to access HTTP headers for the request. The Request object also has a params dictionary containing GET and POST input parameters, as well as a cookies dictionary holding cookie information stored on the client by a PSP application. Notice that the previous example could have just as easily been implemented with a Python CGI script using a Python print statement, but PSP's full benefit becomes clearer in large pages that embed and execute much more complex JPython code to produce a response. PSP runs as a Java servlet and requires the hosting web site to support the Java Servlet API, all of which is beyond the scope of this text. For more details about PSP, visit its web site, currently located at http://www.ciobriefings.com/psp, but search http://www.python.org for other links if this one changes over time. |
I l@ve RuBoard |