[Tutor] CGI File Woes

Noufal Ibrahim noufal at airtelbroadband.in
Sun Sep 30 19:58:22 CEST 2007

wormwood_3 wrote:
> Hello all,
> I am working on a very simple CGI script. The site I want to use it
> on is a shared linux host, but I confirmed that .py files in the
> right dir with the right permissions and shebang execute just fine,
> Hello World sort of tests were successful.

Those are the first things I'd try too.

> So now something a little more involved:
> #!/usr/bin/python2.4
> import cgitb; cgitb.enable()
> thefile = open("template.html", "r")
> templatestuff = thefile.read()
> thefile.close()
> print "Content-Type: text/html"
> if templatestuff:
>     print "Found it"
> title1 = "I am a title!"
> body1 = "I am some hot content"
> print templatestuff % (title1, body1)
> "template.html" is in the same dir, and is simply:
> http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html>
> <html>
>   <head>
>     <title> %s </title>
>   </head>
>   <body>
>                 %s
>   </body>
> </html>
> Any ideas?

Here are a few ideas plus some suggestions. I've been dabbling with 
python/cgi myself and so there might be something you can take from 
these comments.

- If your webserver and the machine where you executed this from are
   different (home directories are NFS mounts etc.), then the permissions
   and directory visibilities might affect things.

- It looks fairly obvious that the problem is in those three lines. Your
   content-type header is printed only after those lines are executed so
   if there is an error there, you'll get an error 500. Try putting your
   content-type line right after your import cgitb.

- Wrap the 3 suspicious lines in a try block and print out the execption
   which you catch.

- Tail the server logs to see what happened.

Good luck.


More information about the Tutor mailing list