home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / d / debugging_ < prev    next >
Text File  |  1996-11-14  |  4KB  |  97 lines

  1. <TITLE>Debugging CGI scripts -- Python library reference</TITLE>
  2. Next: <A HREF="../c/common_problems_and_solutions" TYPE="Next">Common problems and solutions</A>  
  3. Prev: <A HREF="../t/testing_your_cgi_script" TYPE="Prev">Testing your CGI script</A>  
  4. Up: <A HREF="../c/cgi" TYPE="Up">cgi</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>10.1.8. Debugging CGI scripts</H2>
  7. First of all, check for trivial installation errors -- reading the
  8. section above on installing your CGI script carefully can save you a
  9. lot of time.  If you wonder whether you have understood the
  10. installation procedure correctly, try installing a copy of this module
  11. file (<CODE>cgi.py</CODE>) as a CGI script.  When invoked as a script, the file
  12. will dump its environment and the contents of the form in HTML form.
  13. Give it the right mode etc, and send it a request.  If it's installed
  14. in the standard <CODE>cgi-bin</CODE> directory, it should be possible to send it a
  15. request by entering a URL into your browser of the form:
  16. <P>
  17. <UL COMPACT><CODE>    http://yourhostname/cgi-bin/cgi.py?name=Joe+Blow&addr=At+Home<P>
  18. </CODE></UL>
  19. If this gives an error of type 404, the server cannot find the script
  20. -- perhaps you need to install it in a different directory.  If it
  21. gives another error (e.g.  500), there's an installation problem that
  22. you should fix before trying to go any further.  If you get a nicely
  23. formatted listing of the environment and form content (in this
  24. example, the fields should be listed as ``addr'' with value ``At Home''
  25. and ``name'' with value ``Joe Blow''), the <CODE>cgi.py</CODE> script has been
  26. installed correctly.  If you follow the same procedure for your own
  27. script, you should now be able to debug it.
  28. <P>
  29. The next step could be to call the <CODE>cgi</CODE> module's test() function from
  30. your script: replace its main code with the single statement
  31. <P>
  32. <UL COMPACT><CODE>    cgi.test()<P>
  33. </CODE></UL>
  34. This should produce the same results as those gotten from installing
  35. the <CODE>cgi.py</CODE> file itself.
  36. <P>
  37. When an ordinary Python script raises an unhandled exception
  38. (e.g. because of a typo in a module name, a file that can't be opened,
  39. etc.), the Python interpreter prints a nice traceback and exits.
  40. While the Python interpreter will still do this when your CGI script
  41. raises an exception, most likely the traceback will end up in one of
  42. the HTTP server's log file, or be discarded altogether.
  43. <P>
  44. Fortunately, once you have managed to get your script to execute
  45. *some* code, it is easy to catch exceptions and cause a traceback to
  46. be printed.  The <CODE>test()</CODE> function below in this module is an example.
  47. Here are the rules:
  48. <P>
  49. <UL>
  50. <LI>1.   Import the traceback module (before entering the
  51. try-except!)
  52. <P>
  53. <LI>2.   Make sure you finish printing the headers and the blank
  54. line early
  55. <P>
  56. <LI>3.   Assign <CODE>sys.stderr</CODE> to <CODE>sys.stdout</CODE>
  57. <P>
  58. <LI>4.   Wrap all remaining code in a try-except statement
  59. <P>
  60. <LI>5.   In the except clause, call <CODE>traceback.print_exc()</CODE>
  61. </UL>
  62. For example:
  63. <P>
  64. <UL COMPACT><CODE>    import sys<P>
  65.     import traceback<P>
  66.     print "Content-type: text/html"<P>
  67.     print<P>
  68.     sys.stderr = sys.stdout<P>
  69.     try:<P>
  70.         ...your code here...<P>
  71.     except:<P>
  72.         print "\n\n<PRE>"<P>
  73.         traceback.print_exc()<P>
  74. </CODE></UL>
  75. Notes: The assignment to <CODE>sys.stderr</CODE> is needed because the traceback
  76. prints to <CODE>sys.stderr</CODE>.  The <CODE>print "nn<PRE>"</CODE> statement is necessary to
  77. disable the word wrapping in HTML.
  78. <P>
  79. If you suspect that there may be a problem in importing the traceback
  80. module, you can use an even more robust approach (which only uses
  81. built-in modules):
  82. <P>
  83. <UL COMPACT><CODE>    import sys<P>
  84.     sys.stderr = sys.stdout<P>
  85.     print "Content-type: text/plain"<P>
  86.     print<P>
  87.     ...your code here...<P>
  88. </CODE></UL>
  89. This relies on the Python interpreter to print the traceback.  The
  90. content type of the output is set to plain text, which disables all
  91. HTML processing.  If your script works, the raw HTML will be displayed
  92. by your client.  If it raises an exception, most likely after the
  93. first two lines have been printed, a traceback will be displayed.
  94. Because no HTML interpretation is going on, the traceback will
  95. readable.
  96. <P>
  97.