Python CGI checklist: Difference between revisions

From XPUB & Lens-Based wiki
m (Michael Murtaugh moved page PythonCGIChecklist to Python CGI Checklist)
 
No edit summary
Line 1: Line 1:
= Python CGI Checklist =
"Internal server error" got you down...


Checklist for Developing Python CGI Scripts
== Save the file with the extension ".cgi" ==


#  Save the file with the extension ".cgi"
== Seeing the source code? ==


: 2. Seeing the source code? Make sure you are viewing the script via the webserver. In other words, the URL should look like:
Make sure you are viewing the script via a webserver (not as a file). In other words, the URL should start with "http:" not "file:".


<pre>
http://localhost/cgi-bin/script.cgi
: http://localhost/~YOURUSERNAME/script.cgi
http://pzwart3.wdka.hro.nl/~YOU/cgi-bin/script.cgi
</pre>
: not:
<pre>
: file://Users/YOURUSERNAME/Sites/script.cgi
</pre>


If this doesn't work, make sure your webserver is running (Mac OS X: System Preferences, Sharing, Personal Web Sharing should be switched on).
not
:
: 3.#3 First line of the file should be the "shebang" telling the system (Apache) how to run the script (in other words which language it is).


<pre>#!python numbers=off
file://Users/YOURUSERNAME/Sites/script.cgi
#!/usr/bin/python
</pre>


: 4.#4 Permissions must be set to "world-executable".
And if this doesn't work, make sure your webserver is running (Mac OS X: System Preferences, Sharing, Personal Web Sharing should be switched on).


<pre>
== She-Bang! ==
: chmod 705 myscript.cgi
<!--# OR
First line of the file should be the "shebang" telling the system (Apache) how to run the script (in other words which language it is).
-->
: chmod +x myscript.cgi
</pre>


: or simply
#!/usr/bin/python


<pre>
Alternatively you could use the "env" trick + specify a text encoding (in case your code has special characters in it)
: chmod 705 *.cgi
<!--# OR
-->
: chmod +x *.cgi
</pre>


: to enable all the files (in the current directoy) that end in ".cgi"
#!/usr/bin/env python
#-*- coding:utf-8 -*-


== Permissions must be set to "world-executable" ==


: 5.#5 To enable errors to be displayed in the browser, add the following lines to the top of your script:
  chmod +x myscript.cgi
or
  chmod 755 myscript.cgi


<pre>#!python numbers=off
== See python errors in the browser ==
Once the script is actually running, there might of course be something wrong in the code of the program itself. By default, if a python exception occurs, you will see either an Internal Server Error or (worse) a blank page. The ''cgitb'' module (CGI Trackback) will make python errors appear,  and even format them for a browser.
 
<source lang="python">
import cgitb; cgitb.enable()
import cgitb; cgitb.enable()
</pre>
</source>
 
== Print the Content-type first! ==


: 6.#6 Internal Server Error? Ensure the first thing that gets printed is the HTML header:
The very first output of your script must be the "HTTP headers", which at the very least should be a Content-type (with optional character set) ''followed by a blank line''.


<pre>#!python numbers=off
<source lang="python">
print "Content-type: text/html"
print "Content-type: text/html;charset=utf-8"
print
print
</pre>
</source>


: 7.#7 Still an error? Check the webserver's error log. From the terminal in Mac OS X, type:
== Check the webserver's error log ==


<pre>
tail /var/log/apache2/error_log
tail /var/log/httpd/error_log
</pre>


or (gentoo/apache2):
or maybe..


<pre>
tail /var/log/httpd/error_log
tail /var/log/apache2/error_log
</pre>

Revision as of 12:19, 13 May 2013

"Internal server error" got you down...

Save the file with the extension ".cgi"

Seeing the source code?

Make sure you are viewing the script via a webserver (not as a file). In other words, the URL should start with "http:" not "file:".

http://localhost/cgi-bin/script.cgi
http://pzwart3.wdka.hro.nl/~YOU/cgi-bin/script.cgi

not

file://Users/YOURUSERNAME/Sites/script.cgi

And if this doesn't work, make sure your webserver is running (Mac OS X: System Preferences, Sharing, Personal Web Sharing should be switched on).

She-Bang!

First line of the file should be the "shebang" telling the system (Apache) how to run the script (in other words which language it is).

#!/usr/bin/python

Alternatively you could use the "env" trick + specify a text encoding (in case your code has special characters in it)

#!/usr/bin/env python
#-*- coding:utf-8 -*-

Permissions must be set to "world-executable"

 chmod +x myscript.cgi

or

 chmod 755 myscript.cgi

See python errors in the browser

Once the script is actually running, there might of course be something wrong in the code of the program itself. By default, if a python exception occurs, you will see either an Internal Server Error or (worse) a blank page. The cgitb module (CGI Trackback) will make python errors appear, and even format them for a browser.

import cgitb; cgitb.enable()

Print the Content-type first!

The very first output of your script must be the "HTTP headers", which at the very least should be a Content-type (with optional character set) followed by a blank line.

print "Content-type: text/html;charset=utf-8"
print

Check the webserver's error log

tail /var/log/apache2/error_log

or maybe..

tail /var/log/httpd/error_log