![]() | Exercise 2.9. Tracing | ||
|
You can put print statements that will display trace information in the server log file (for instance /var/log/apache/error.log), by writing on the sys.stderr file handle:
#! /local/bin/python
import sys
query = cgi.parse()
print >>sys.stderr, "(debug from " + sys.argv[0] + ") second field: ", query['second']
| |||
![]() | Exercise 2.10. Redirecting standard error to the browser | ||
|
Another very useful feature is to be able to access Python error messages directly on the CGI output, instead of the server log file. The way to achieve this is to redirect sys.stderr to sys.stdout:
#! /local/bin/python
import sys
sys.stderr = sys.stdout
| |||
![]() | Exercise 2.12. Debugging your script directly |
|
An easy way to debug your script can be to run it directly from the Unix prompt: % ./test.pybut in this case, you need to simulate input from the Web server (and you will have many environment variables unset). One way to achieve this can be: % QUERY_STRING='first=val1&second=val2' ./test.py | |