16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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 />

<strong>Python</strong>Debug On<br />

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

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

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

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

with a URL ending in .cgi. mod_python converts the .cgi <strong>to</strong> a .py when it looks for a file <strong>to</strong> 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 <strong>Python</strong> Server Pages, should be familiar. PSP documents are a mix<br />

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

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

calls <strong>to</strong> an output function.<br />

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

.htaccess file:<br />

AddHandler mod_python .psp<br />

<strong>Python</strong>Handler 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 <strong>Python</strong>Debug 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 <strong>to</strong> 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 <strong>to</strong> 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 in<strong>to</strong> 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!