Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional

16.01.2014 Views

CHAPTER 15 ■ PYTHON AND THE WEB 331 For debugging information (which can be useful when something goes wrong, as it usually will), you can add the following: PythonDebug On You should remove this directive when you’re done developing; there’s no point in exposing the innards of your program to the (potentially malevolent) public. Once you’ve set things up properly, you should be able to run your CGI scripts just like before. ■Note In order to get this to work, you might need to give your script a .py ending, even if you access it with a URL ending in .cgi. mod_python converts the .cgi to a .py when it looks for a file to fulfill the request. PSP If you’ve used PHP (the PHP: Hypertext Preprocessor, originally known as Personal Home Page), Microsoft ASP (Active Server Pages), JSP (JavaServer Pages), or something similar, the concepts underlying PSP, or Python Server Pages, should be familiar. PSP documents are a mix of HTML (or, for that matter, some other form of document) and Python code, with the Python code enclosed in special-purpose tags. Any HTML (or other plain data) will be converted to calls to an output function. Setting Apache up to serve your PSP pages is as simple as putting the following in your .htaccess file: AddHandler mod_python .psp PythonHandler mod_python.psp This will treat files with the .psp file extension as PSP files. ■Caution While developing your PSP pages, using the directive PythonDebug On can be useful. You should not, though, keep it on when the system is used for real, because any error in the PSP page will result in an exception traceback including the source code being served to the user. Letting a potentially hostile user see the source code of your program is something that should not be done lightly. If you publish the code deliberately, others may help you find security flaws, and this can definitely be one of the strong sides to open source software development. However, simply letting users glimpse your code through error messages is probably not useful, and potentially a security risk. There are two main sets of PSP tags: one set for statements, another for expressions. The values of expressions in expression tags are directly put into the output document. Listing 15-8 is a simple PSP example, which first performs some setup code (statements) and then outputs some random data as part of the Web page, using an expression tag.

332 CHAPTER 15 ■ PYTHON AND THE WEB Listing 15-8. A Slightly Stochastic PSP Example Hello Hello, world. My name is Mr. Gumby. You can mix plain output, statements, and expressions in any way you like. You can write comments (that will not be part of the output) . There is really very little to PSP programming beyond these basics. You need to be aware of one issue, though: If code in a statement tag starts an indented block, the block will persist, with the following HTML being put inside the block. One way to close such a block is to insert a comment, as in the following: A merry, merry christmas time. In general, if you’ve used PHP or JSP or the like, you will probably notice that PSP is more picky about newlines and indentation; this is, of course, a feature inherited from Python itself. There are many, many other systems that somewhat resemble mod_python’s PSP, and even some that are almost identical (such as the Webware PSP system, available from http:// webwareforpython.org) or similarly named, but with a rather different syntax (such as the Spyce PSP, available from http://spyce.sf.net). The Web development system Zope (see http:// zope.org) has its own template languages (such as ZPT). The rather innovative template system ClearSilver (see http://clearsilver.net) has Python bindings, and could be an interesting alternative for the curious. A visit to the Parnassus Web category (http://py.vaults.ca/apyllo. py?i=127386987) or a Web search for “python template system” (or something similar) should point you toward several other interesting systems. The Publisher This is where mod_python really comes into its own: It lets you write Python programs that have a much more interesting environment than CGI scripts. To use the publisher handler, put the following in your .htaccess file (again, optionally adding PythonDebug On while you’re developing): AddHandler mod_python .py PythonHandler mod_python.publisher

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

Listing 15-8. A Slightly S<strong>to</strong>chastic 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 <strong>to</strong><br />

PSP programming beyond these basics. You need <strong>to</strong> 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 <strong>to</strong> close such a block is <strong>to</strong> 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 <strong>Python</strong> 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 <strong>Python</strong> bindings, and could be an interesting<br />

alternative for the curious. A visit <strong>to</strong> 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 <strong>to</strong>ward several other interesting systems.<br />

The Publisher<br />

This is where mod_python really comes in<strong>to</strong> its own: It lets you write <strong>Python</strong> 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 <strong>Python</strong>Debug On while you’re<br />

developing):<br />

AddHandler mod_python .py<br />

<strong>Python</strong>Handler mod_python.publisher

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

Saved successfully!

Ooh no, something went wrong!