Re: [Tutor] CGI File Woes

2007-09-30 Thread Eric Walker
The best way to debug cgi is to tail the server log when you don't get 
commandline output. This will give you a clue to what is happening.. shoulld be 
something like /var/log/apache or something like that...
Eric




wormwood_3 [EMAIL PROTECTED] 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;


  
 %s 
  
  
%s
  


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@python.org
http://mail.python.org/mailman/listinfo/tutor


   
-
Catch up on fall's hot new shows on Yahoo! TV.  Watch previews, get listings, 
and more!___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread Alan Gauld

wormwood_3 [EMAIL PROTECTED] wrote

 #!/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

Did you mean for there to be two html tags or is it a typo?

  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,

 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.

I've never used gcitb (and until now didn't know it existed!)
so can't comment.

But I usually see this when the web user doesn't have
permission to open the html file. But...

  I have confirmed the file has the right permissions,
 I have even tried it with all permissions set on that file.

For all users?
Remember that the web server runs as a separate user
and that's the user than needs to open the file.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread Michael Langford
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 [EMAIL PROTECTED] 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@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
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread wormwood_3
Did you mean for there to be two html tags or is it a typo?

Just a typo. Should not have a big effect, but it has been fixed.

I've never used cgitb (and until now didn't know it existed!)
so can't comment.

I had not heard of it until this week when I started working on CGI stuff, but 
I have found it super handy! All you have to do it import cgitb; 
cgitb.enable() and all tracebacks will get printed to nicely formatted HTML 
output on the page you were trying to load.

But I usually see this when the web user doesn't have
permission to open the html file. But...

This definitely would make the most sense in that the error is not a Python 
error, at least I am assuming so from the lack of traceback. However, the odd 
thing is that if I hit template.html directly, the page renders just fine. This 
would mean the web user can access it, right? And I was assuming the Python 
script was being run by the web user as well, but perhaps it is not.

For all users?
Remember that the web server runs as a separate user
and that's the user than needs to open the file.

Sorry, I stated that poorly. What I meant was that in the process of 
troubleshooting, I ended up enabling ALL permissions, so user, group, and 
others could read, write, and execute. So since this is now the case, and the 
script runs fine locally, and I can hit template.html on its own on the web 
server... I am still quite baffled!

-Sam



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread Alan Gauld

wormwood_3 [EMAIL PROTECTED] wrote 

 I had not heard of it until this week when I started working on 
 CGI stuff, but I have found it super handy! All you have to do 
 it import cgitb; cgitb.enable() and all tracebacks will get 
 printed to nicely formatted HTML output on the page you 
 were trying to load.

Ok, Heres a thought.

try forcing an error after opening the file.
See what the traceback says. You can add a message 
string to an exception so that it will print...

class LogError(Exception): pass

raise LogError, Any old text here

So you force the cgitb to kick in and print the mesage you 
specify, use it like a print statement to debug whats happening...

Its a bit laborious but better than nothing!

See if that helps

Alan G




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread Noufal Ibrahim
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.




-- 
~noufal
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread Martin Walsh
wormwood_3 wrote:
 I've never used cgitb (and until now didn't know it existed!)
 so can't comment.
 
 I had not heard of it until this week when I started working on CGI stuff, 
 but I have found it super handy! All you have to do it import cgitb; 
 cgitb.enable() and all tracebacks will get printed to nicely formatted HTML 
 output on the page you were trying to load.
 

No doubt cgitb is a great tool for debugging cgi, but IIUC there are at
least two instances when you will not get the pretty printed tracebacks
in the browser when using cgitb. One is after, what I would call, a
'compile time' exception such as SyntaxError, in your python code. The
other is when the python code runs without exception, but you have not
separated the header and the document content with a newline. At least,
I have found these to be true when using apache-cgi.

Consider the following examples:

#!/usr/bin/env python
# raises a SyntaxError

import cgi
import cgitb; cgitb.enable()

print Content-type: text/html\n\n
# NOTE: purposeful misspelling of the print statement
prin htmlbodypHello, world!/p/body/html

# the code above will produce a server 500, with apache
# complaining about premature end of script headers

...

#!/usr/bin/env python
# good python, bad data

import cgi
import cgitb; cgitb.enable()

print Content-type: text/html
print htmlbodypHello, world!/p/body/html

# this is syntactically correct python, and will
# run from the command line, but the html header
# and html content have no separation, so apache
# will consider all of it to be header info, resulting
# in another server 500, malformed header

As others have advised, under these circumstances you can review the
apache error log (if your webhost allows it). Or roll-your-own logging
equivalent, and run your cgi from a terminal to catch SyntaxErrors and
the like.

HTH,
Marty
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread wormwood_3
Those examples were a lot of help Martin. Turned out the only issue was that I 
did not have this line right:

print Content-type: text/html\n\n

With that form, it loaded just fine. It had been running fine from the 
terminal, but without this line being right, Apache did not know what to do 
with it.

I had spoken with my web-hosting provider, but since I had a shared account I 
was unable to view the server logs. And the person helping me knew nothing 
about Python (he kept slipping and calling it PHP actually, to my dismay and 
chagrin:-) ).

Thanks for all the help, Alan and Martin.

-Sam

_
- Original Message 
From: Martin Walsh [EMAIL PROTECTED]
To: tutor@python.org
Sent: Sunday, September 30, 2007 1:07:02 PM
Subject: Re: [Tutor] CGI File Woes

No doubt cgitb is a great tool for debugging cgi, but IIUC there are at
least two instances when you will not get the pretty printed tracebacks
in the browser when using cgitb. One is after, what I would call, a
'compile time' exception such as SyntaxError, in your python code. The
other is when the python code runs without exception, but you have not
separated the header and the document content with a newline. At least,
I have found these to be true when using apache-cgi.

Consider the following examples:

#!/usr/bin/env python
# raises a SyntaxError

import cgi
import cgitb; cgitb.enable()

print Content-type: text/html\n\n
# NOTE: purposeful misspelling of the print statement
prin htmlbodypHello, world!/p/body/html

# the code above will produce a server 500, with apache
# complaining about premature end of script headers

...

#!/usr/bin/env python
# good python, bad data

import cgi
import cgitb; cgitb.enable()

print Content-type: text/html
print htmlbodypHello, world!/p/body/html

# this is syntactically correct python, and will
# run from the command line, but the html header
# and html content have no separation, so apache
# will consider all of it to be header info, resulting
# in another server 500, malformed header

As others have advised, under these circumstances you can review the
apache error log (if your webhost allows it). Or roll-your-own logging
equivalent, and run your cgi from a terminal to catch SyntaxErrors and
the like.

HTH,
Marty
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] CGI File Woes

2007-09-30 Thread Martin Walsh
wormwood_3 wrote:
 Those examples were a lot of help Martin. Turned out the only issue was that 
 I did not have this line right:
 
 print Content-type: text/html\n\n

Sam,

Glad the examples helped. I should probably fess up and point out that I
mistakenly added an additional newline to that print statement. You only
need one, as the print itself will append an extra. So by adding the
addition \n, a blank line will be sent to the browser as part of the
page content, which shouldn't be a problem for text/html content-types.
But as soon as you want to serve up another content-type (eg. image/png)
you may run into odd problems.

So, the corrected print statement should look like this:

print Content-type: text/html\n

or as described in the docs, http://docs.python.org/lib/cgi-intro.html

print Content-type: text/html
print

HTH,
Marty

 
 With that form, it loaded just fine. It had been running fine from the 
 terminal, but without this line being right, Apache did not know what to do 
 with it.
 
 I had spoken with my web-hosting provider, but since I had a shared account I 
 was unable to view the server logs. And the person helping me knew nothing 
 about Python (he kept slipping and calling it PHP actually, to my dismay and 
 chagrin:-) ).
 
 Thanks for all the help, Alan and Martin.
 
 -Sam
 
 _
 - Original Message 
 From: Martin Walsh [EMAIL PROTECTED]
 To: tutor@python.org
 Sent: Sunday, September 30, 2007 1:07:02 PM
 Subject: Re: [Tutor] CGI File Woes
 
 No doubt cgitb is a great tool for debugging cgi, but IIUC there are at
 least two instances when you will not get the pretty printed tracebacks
 in the browser when using cgitb. One is after, what I would call, a
 'compile time' exception such as SyntaxError, in your python code. The
 other is when the python code runs without exception, but you have not
 separated the header and the document content with a newline. At least,
 I have found these to be true when using apache-cgi.
 
 Consider the following examples:
 
 #!/usr/bin/env python
 # raises a SyntaxError
 
 import cgi
 import cgitb; cgitb.enable()
 
 print Content-type: text/html\n\n
 # NOTE: purposeful misspelling of the print statement
 prin htmlbodypHello, world!/p/body/html
 
 # the code above will produce a server 500, with apache
 # complaining about premature end of script headers

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor