CGI and logging module

P

Peter Mott

I am using:

Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4

with Apache/1.3.26. The following script causes an Internal Server Error
(with nothing in the Apache error logs AFAIK):

#!/usr/local/bin/python

import logging

print "Content-type: text/html\n\n"
print "<html><head><title></title></head><body><h1>OK</h1>"""
print "</body></html>"

The same script with the import statement commented out works as expected.
Has anyone encountered this? I have googled without success.

Thanks

Peter
 
P

Peter Hansen

Peter said:
I am using:

Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4

with Apache/1.3.26. The following script causes an Internal Server Error
(with nothing in the Apache error logs AFAIK):

#!/usr/local/bin/python

import logging

print "Content-type: text/html\n\n"
print "<html><head><title></title></head><body><h1>OK</h1>"""
print "</body></html>"

The same script with the import statement commented out works as expected.
Has anyone encountered this? I have googled without success.

Can't help on the specific problem, but two points:

1. You are supposed to be returning \r\n, I believe, not just
\n between lines.

2. If you want to see what is going wrong, put a try/except
around the import statement and try to write something useful
along with your results:

err = ''
try:
import logging
except Exception, ex:
err = str(ex)

print 'Content-type: test/html\r\n\r\n'
blah blah blah
print '<p>Error: %s</p>\r\n'

If you can manage to import the sys and traceback modules,
you can get a more fully formatted exception using
''.join(traceback.format_exception(*sys.exc_info()))

-Peter
 
P

Peter Otten

Peter said:
2. If you want to see what is going wrong, put a try/except
around the import statement and try to write something useful
along with your results:

err = ''
try:
import logging
except Exception, ex:
err = str(ex)

print 'Content-type: test/html\r\n\r\n'
blah blah blah
print '<p>Error: %s</p>\r\n'

If you can manage to import the sys and traceback modules,
you can get a more fully formatted exception using
''.join(traceback.format_exception(*sys.exc_info()))

I've just discovered the cgitb module, which offers a minimally intrusive
way to debug cgi scripts - just add

import cgitb
cgitb.enable()

at the top of the script.

(Yet another)
Peter
 
A

Andrew James

Peter,
Have you considered using mod_python as an alternative to CGI? It offers
a number of benefits including partial session support, integration into
Apache's internals and caching bytecode. I can provide you with some
sample code if you wish (although the online documentation is very good)

Regards,
Andrew
 
P

Peter Mott

Thanks for these responses all useful. I had completed a fairly large script
which threw these unrecorded errors at me. Took ages to locate the soiurce
of the problem. I should have used the try: except: round the import
statement (I was using the 'import cgitb;cgitb.enable()' mantra but _after_
the impoprt statement :-( ).

As I said I am using Python on FreeBSD:

Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.
and I imported loggng successfully. When I modify my script to the
following:
===============
#!/usr/local/bin/python

try:
import logging
except Exception, e:
print "Content-type: text/html\n"
print "<html><head><title></title></head><body><p>" + str(e) +"<p>"""
else:
print "Content-type: text/html\n"
print
"<html><head><title></title></head><body><h1>OK</h1></body></html>"""

==============
It reports the error which is " No module named logging " . Something is
wrong here. Does anyone have any ideas?

Peter



Peter Mott said:
I am using:

Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4

with Apache/1.3.26. The following script causes an Internal Server Error
(with nothing in the Apache error logs AFAIK):

#!/usr/local/bin/python

import logging

print "Content-type: text/html\n\n"
print "<html><head><title></title></head><body><h1>OK</h1>"""
print "</body></html>"

The same script with the import statement commented out works as expected.
Has anyone encountered this? I have googled without success.

Thanks

Peter
 
P

Peter Otten

Andrew said:
Peter,
Have you considered using mod_python as an alternative to CGI? It offers
a number of benefits including partial session support, integration into
Apache's internals and caching bytecode. I can provide you with some
sample code if you wish (although the online documentation is very good)

As you may have noted, "Peter" is a bit ambiguous in this thread :)
If you meant to address me, I use CGIs only for personal purposes on the
local machine - basically as a substitute for command line tools with fancy
output. mod_python's CGI handler does not seem to offer any advantage here,
or does it?

Peter
 
P

Peter Otten

Peter said:
As I said I am using Python on FreeBSD:

Python 2.3.4 (#2, Nov 14 2004, 18:06:48)
[GCC 2.95.4 20020320 [FreeBSD]] on freebsd4
Type "help", "copyright", "credits" or "license" for more information.
and I imported loggng successfully. When I modify my script to the
following:
===============
#!/usr/local/bin/python

try:
import logging
except Exception, e:
print "Content-type: text/html\n"
print "<html><head><title></title></head><body><p>" + str(e) +"<p>"""
else:
print "Content-type: text/html\n"
print
"<html><head><title></title></head><body><h1>OK</h1></body></html>"""

==============
It reports the error which is " No module named logging " . Something is
wrong here. Does anyone have any ideas?

Is 2.3.4 the only python interpreter on your system? You could use
sys.version to verify that you are not accidentally using an older (pre
2.3) version that does not offer that package.

Peter
 
?

=?ISO-8859-1?Q?Michael_Str=F6der?=

Peter said:
import cgitb
cgitb.enable()

But make sure to turn it off when going public with your service since
it reveals interesting information to an attacker.

Ciao, Michael.
 
P

Peter Mott

This was a Columbus Egg. The version of Python Apache used had reverted to
2.2.2 after a recent system failure. The version used for console logins was
2.3.4. I'll contact my ISP :). Thanks for all the help.

Peter
 
A

Andrew Clover

Peter Mott said:
The same script with the import statement commented out works as expected.
Has anyone encountered this?

You'll need to find out what the error is to go further (see other
posts) but as a tip: problems like this are sometimes caused by
insufficient permissions.

If you have root, try 'su'-ing to the Apache user (eg. 'su nobody')
and running the script directly ('./script.py') - does it work?
 
P

Peter Hansen

Peter said:
This was a Columbus Egg. The version of Python Apache used had reverted to
2.2.2 after a recent system failure. The version used for console logins was
2.3.4. I'll contact my ISP :). Thanks for all the help.

Cool, glad to help. :)

I was just going to point out that importing builtin modules such
as 'sys' should "always" work, so you could print the values of
sys.modules, sys.prefix, sys.executable, and so forth, to get
a fix on what was what. Looks like you didn't need that though.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,211
Messages
2,571,092
Members
47,694
Latest member
digiadvancemarketing

Latest Threads

Top