[Tutor] CGI File Woes

Michael Langford mlangford.cs03 at gtalumni.org
Sun Sep 30 16:15:27 CEST 2007


Remember, you can always add more logging that goes to your own log file.

Have it open a file in /tmp and write out a log of what's happening.
Wrap your main code block in an exception handler, etc. Have it print
out the exception to the log file.

This will allow you to look and see what the error is rather than
speculating about it.

             --Michael

On 9/30/07, wormwood_3 <wormwood_3 at yahoo.com> 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.
>
> 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>
>
> If I run this script without the 3 lines after the import line, it works fine (namely I get an error that templatestuff is not defined, as would be expected). With those lines however, I am getting a 500 Internal Server Error. Since I am not shown an error page with cgitb, this would likely mean a syntax error. However, if I run the script locally, it works just fine, printing out the HTML with variables filled in. Now for the odd part: If I change that open line to "thefile = open("asdas", "r")", I get "IOError: [Errno 2] No such file or directory: 'asdas'
> ". So it seems the script is finding the template file when I have it as above, but is throwing something when it tries to open it. I have confirmed the file has the right permissions, I have even tried it with all permissions set on that file.
>
> I am just totally baffled why I cannot open any files from the script.
>
> Any ideas?
> -Sam
>
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com


More information about the Tutor mailing list