04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

332 CHAPTER 15 ■ PYTHON AND THE WEB<br />

Listing 15-8. A Slightly Stochastic PSP Example<br />

<br />

<br />

<br />

Hello<br />

<br />

<br />

Hello, world. My name is Mr. Gumby.<br />

<br />

<br />

You can mix plain output, statements, and expressions in any way you like. You can write<br />

comments (that will not be part of the output) . There is really very little to<br />

PSP programming beyond these basics. You need to be aware of one issue, though: If code in a<br />

statement tag starts an indented block, the block will persist, with the following HTML being<br />

put inside the block. One way to close such a block is to insert a comment, as in the following:<br />

A merry, merry christmas time.<br />

In general, if you’ve used PHP or JSP or the like, you will probably notice that PSP is more<br />

picky about newlines and indentation; this is, of course, a feature inherited from Python itself.<br />

There are many, many other systems that somewhat resemble mod_python’s PSP, and<br />

even some that are almost identical (such as the Webware PSP system, available from http://<br />

webwareforpython.org) or similarly named, but with a rather different syntax (such as the Spyce<br />

PSP, available from http://spyce.sf.net). The Web development system Zope (see http://<br />

zope.org) has its own template languages (such as ZPT). The rather innovative template system<br />

ClearSilver (see http://clearsilver.net) has Python bindings, and could be an interesting<br />

alternative for the curious. A visit to the Parnassus Web category (http://py.vaults.ca/apyllo.<br />

py?i=127386987) or a Web search for “python template system” (or something similar) should<br />

point you toward several other interesting systems.<br />

The Publisher<br />

This is where mod_python really comes into its own: It lets you write Python programs that<br />

have a much more interesting environment than CGI scripts. To use the publisher handler, put<br />

the following in your .htaccess file (again, optionally adding PythonDebug On while you’re<br />

developing):<br />

AddHandler mod_python .py<br />

PythonHandler mod_python.publisher

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!