Python CGI checklist: Difference between revisions

From XPUB & Lens-Based wiki
m (Michael Murtaugh moved page PythonCGIChecklist to Python CGI Checklist)
 
m (Michael Murtaugh moved page CGI checklist to Python CGI checklist over redirect)
 
(27 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= Python CGI Checklist =
[[File:InternalServerErrorNegate.png]]


Checklist for Developing Python CGI Scripts
"Internal server error" got you down... no problem, just check the following


#  Save the file with the extension ".cgi"
== Use extension ".cgi" ==


: 2. Seeing the source code? Make sure you are viewing the script via the webserver. In other words, the URL should look like:
The default extension for CGI on a web server is .cgi.


<pre>
== She-Bang! ==
: http://localhost/~YOURUSERNAME/script.cgi
</pre>
The very 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).
: 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).
<source lang="python">
:  
#!/usr/bin/python
: 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).
</source>
 
Even better, you could use the "env" trick (finds whatever python is available) + also specify a text encoding while you're at it (in case your code has special characters in it)
 
<source lang="python">
#!/usr/bin/env python
#-*- coding:utf-8 -*-
</source>
 
== chmod +x ==
 
Permissions must be set to "world-executable". You can maybe set this via an (s)ftp program, but the simplest / most failsafe way is directly via the shell:


<pre>#!python numbers=off
  chmod +x myscript.cgi
#!/usr/bin/python
or
</pre>
  chmod 755 myscript.cgi


: 4.#4 Permissions must be set to "world-executable".
== Print the Content-type first! ==


<pre>
The '''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''' (which terminates the headers section). After that, all output is considered the "contents" of the document.
: chmod 705 myscript.cgi
<!--# OR
-->
: chmod +x myscript.cgi
</pre>


: or simply
<source lang="python">
print "Content-type: text/html;charset=utf-8"
print
</source>


<pre>
== Troubleshooting ==
: chmod 705 *.cgi
=== Make sure you are viewing the script through HTTP / a web server (and not as a local file) ===
<!--# OR
-->
: chmod +x *.cgi
</pre>


: to enable all the files (in the current directoy) that end in ".cgi"
It's the web server that actually performs a CGI script -- not the browser, so make sure you're looking at the script ''via a webserver'' rather than directly opening the script in your browser. (This would be one reason you might be seeing the raw source code of the script rather than it's output). In any case the URL (in the browser) should start with "http:" not "file:".


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


: 5.#5 To enable errors to be displayed in the browser, add the following lines to the top of your script:
''not''


<pre>#!python numbers=off
file:///this/can't/be/right/script.cgi
import cgitb; cgitb.enable()
</pre>


: 6.#6 Internal Server Error? Ensure the first thing that gets printed is the HTML header:
See [[Serve.py]] for a tip on how to run a simple local CGI-capable webserver with a simple python command.


<pre>#!python numbers=off
=== Display python errors in the browser ===
print "Content-type: text/html"
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 formats them for a browser.
print
</pre>


: 7.#7 Still an error? Check the webserver's error log. From the terminal in Mac OS X, type:
<source lang="python">
import cgitb; cgitb.enable()
</source>


<pre>
=== Check the webserver's error log ===
tail /var/log/httpd/error_log
</pre>


or (gentoo/apache2):
Use a shell on the server machine (so ssh to pzwart if that's where you're running) and:


<pre>
tail /var/log/apache2/error.log
tail /var/log/apache2/error_log
</pre>

Latest revision as of 11:34, 12 February 2014

InternalServerErrorNegate.png

"Internal server error" got you down... no problem, just check the following

Use extension ".cgi"

The default extension for CGI on a web server is .cgi.

She-Bang!

The very 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

Even better, you could use the "env" trick (finds whatever python is available) + also specify a text encoding while you're at it (in case your code has special characters in it)

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

chmod +x

Permissions must be set to "world-executable". You can maybe set this via an (s)ftp program, but the simplest / most failsafe way is directly via the shell:

 chmod +x myscript.cgi

or

 chmod 755 myscript.cgi

Print the Content-type first!

The 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 (which terminates the headers section). After that, all output is considered the "contents" of the document.

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

Troubleshooting

Make sure you are viewing the script through HTTP / a web server (and not as a local file)

It's the web server that actually performs a CGI script -- not the browser, so make sure you're looking at the script via a webserver rather than directly opening the script in your browser. (This would be one reason you might be seeing the raw source code of the script rather than it's output). In any case the URL (in the browser) should start with "http:" not "file:".

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

not

file:///this/can't/be/right/script.cgi

See Serve.py for a tip on how to run a simple local CGI-capable webserver with a simple python command.

Display 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 formats them for a browser.

import cgitb; cgitb.enable()

Check the webserver's error log

Use a shell on the server machine (so ssh to pzwart if that's where you're running) and:

tail /var/log/apache2/error.log