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.

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

For debugging information (which can be useful when something goes wrong, as it usually<br />

will), you can add the following:<br />

PythonDebug On<br />

You should remove this directive when you’re done developing; there’s no point in exposing<br />

the innards of your program to the (potentially malevolent) public.<br />

Once you’ve set things up properly, you should be able to run your CGI scripts just like before.<br />

■Note In order to get this to work, you might need to give your script a .py ending, even if you access it<br />

with a URL ending in .cgi. mod_python converts the .cgi to a .py when it looks for a file to fulfill the request.<br />

PSP<br />

If you’ve used PHP (the PHP: Hypertext Preprocessor, originally known as Personal Home<br />

Page), Microsoft ASP (Active Server Pages), JSP (JavaServer Pages), or something similar, the<br />

concepts underlying PSP, or Python Server Pages, should be familiar. PSP documents are a mix<br />

of HTML (or, for that matter, some other form of document) and Python code, with the Python<br />

code enclosed in special-purpose tags. Any HTML (or other plain data) will be converted to<br />

calls to an output function.<br />

Setting Apache up to serve your PSP pages is as simple as putting the following in your<br />

.htaccess file:<br />

AddHandler mod_python .psp<br />

PythonHandler mod_python.psp<br />

This will treat files with the .psp file extension as PSP files.<br />

■Caution While developing your PSP pages, using the directive PythonDebug On can be useful. You<br />

should not, though, keep it on when the system is used for real, because any error in the PSP page will result<br />

in an exception traceback including the source code being served to the user. Letting a potentially hostile user<br />

see the source code of your program is something that should not be done lightly. If you publish the code<br />

deliberately, others may help you find security flaws, and this can definitely be one of the strong sides to open<br />

source software development. However, simply letting users glimpse your code through error messages is<br />

probably not useful, and potentially a security risk.<br />

There are two main sets of PSP tags: one set for statements, another for expressions. The<br />

values of expressions in expression tags are directly put into the output document. Listing 15-8 is a<br />

simple PSP example, which first performs some setup code (statements) and then outputs<br />

some random data as part of the Web page, using an expression tag.

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

Saved successfully!

Ooh no, something went wrong!