Python CGI script runs in shell but not from web

software development

#1

Hello,

I am just getting started with python cgi programming. I started out rather ambitiously with a python script that ran but failed. I have since been whitling it down to barebones, and somewhere along the way it stopped executing altogether. I now get the following error when I try to run any python script.

[quote]"Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request."[/quote]

My python file currently consists of (only):

[code]#!/usr/bin/python

import cgi, os
import cgitb; cgitb.enable()

#form = cgi.FieldStorage() # instantiate only once!

print “” # HTML is following
print ""
print # blank line, end of headers

print "CGI script output"
print ""
print "

This is my first CGI script

"
print “Hello, world!”

#print "
#for key in form.keys():

print key

print “”
[/code]
If I run it from the shell it runs fine, but gives the above error when I call it from a web browser, either directly or in a post statement on a web form.

I’ve also run dos2unix on it from the shell just because, but that didn’t help.

Any ideas?

cheers,

Marc Pelletier


#2

The “headers” your script is generating are invalid. CGI headers need to follow the form:

[code]Header-Name: value
Another-Header-Name: another value…

Your script output[/code]

One header that you’ll virtually always want is Content-Type:

Try outputting that and see what happens. If that still gives you a 500 error, check your site’s error.log (in ~/logs/example.com/http/error.log) for details on what’s going wrong.


#3

Thanks Andrewf,

Once I implemented that, and corrected all the typos. It works!

cheers,
Marc