471,075 Members | 799 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

CGI and logging module

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
Jul 18 '05 #1
10 3501
Peter Mott wrote:
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
Jul 18 '05 #2
Peter Hansen wrote:
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

Jul 18 '05 #3
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

On Thu, 2004-11-18 at 22:37 +0100, Peter Otten wrote:
Peter Hansen wrote:
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

--
Andrew James <dr**@gremlinhosting.com>

Jul 18 '05 #4
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.
import logging

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" <pe***@monicol.co.uk> wrote in message
news:41*********************@news.gradwell.net...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

Jul 18 '05 #5
Andrew James wrote:
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
Jul 18 '05 #6
Peter Mott wrote:
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.
import logging


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

Jul 18 '05 #7
Peter Otten wrote:

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.
Jul 18 '05 #8
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

"Peter Mott" <pe***@monicol.co.uk> wrote in message
news:41*********************@news.gradwell.net...
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

Jul 18 '05 #9
Peter Mott <pe***@monicol.co.uk> wrote:
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?

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/
Jul 18 '05 #10
Peter Mott wrote:
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
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Robert.Schmitt | last post: by
8 posts views Thread by Steve Erickson | last post: by
6 posts views Thread by Ville Vainio | last post: by
23 posts views Thread by Rotem | last post: by
3 posts views Thread by Chris Smith | last post: by
7 posts views Thread by Jed Parsons | last post: by
3 posts views Thread by Lowell Alleman | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.