Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
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
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313 and 314: 282 CHAPTER 12 ■ GRAPHICAL USER I
- Page 316 and 317: CHAPTER 13 ■ ■ ■ Database Sup
- Page 318 and 319: CHAPTER 13 ■ DATABASE SUPPORT 287
- Page 320 and 321: CHAPTER 13 ■ DATABASE SUPPORT 289
- Page 322 and 323: CHAPTER 13 ■ DATABASE SUPPORT 291
- Page 324 and 325: CHAPTER 13 ■ DATABASE SUPPORT 293
- Page 326: CHAPTER 13 ■ DATABASE SUPPORT 295
- Page 329 and 330: 298 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 331 and 332: 300 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 333 and 334: 302 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 335 and 336: 304 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 337 and 338: 306 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 339 and 340: 308 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 341 and 342: 310 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 343 and 344: 312 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 345 and 346: 314 CHAPTER 15 ■ PYTHON AND THE W
- Page 347 and 348: 316 CHAPTER 15 ■ PYTHON AND THE W
- Page 349 and 350: 318 CHAPTER 15 ■ PYTHON AND THE W
- Page 351 and 352: 320 CHAPTER 15 ■ PYTHON AND THE W
- Page 353 and 354: 322 CHAPTER 15 ■ PYTHON AND THE W
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
- Page 357 and 358: 326 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361: 330 CHAPTER 15 ■ PYTHON AND THE W
- Page 365 and 366: 334 CHAPTER 15 ■ PYTHON AND THE W
- Page 367 and 368: 336 CHAPTER 15 ■ PYTHON AND THE W
- Page 369 and 370: 338 CHAPTER 15 ■ PYTHON AND THE W
- Page 372 and 373: CHAPTER 16 ■ ■ ■ Testing, 1-2
- Page 374 and 375: CHAPTER 16 ■ TESTING, 1-2-3 343 W
- Page 376 and 377: CHAPTER 16 ■ TESTING, 1-2-3 345 d
- Page 378 and 379: CHAPTER 16 ■ TESTING, 1-2-3 347 u
- Page 380 and 381: CHAPTER 16 ■ TESTING, 1-2-3 349 F
- Page 382 and 383: CHAPTER 16 ■ TESTING, 1-2-3 351 P
- Page 384 and 385: CHAPTER 16 ■ TESTING, 1-2-3 353 "
- Page 386: CHAPTER 16 ■ TESTING, 1-2-3 355 N
- Page 389 and 390: 358 CHAPTER 17 ■ EXTENDING PYTHON
- Page 391 and 392: 360 CHAPTER 17 ■ EXTENDING PYTHON
- Page 393 and 394: 362 CHAPTER 17 ■ EXTENDING PYTHON
- Page 395 and 396: 364 CHAPTER 17 ■ EXTENDING PYTHON
- Page 397 and 398: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 399 and 400: 368 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401 and 402: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 404 and 405: CHAPTER 18 ■ ■ ■ Packaging Yo
- Page 406 and 407: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 408 and 409: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 410 and 411: CHAPTER 18 ■ PACKAGING YOUR PROGR
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