473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 3651
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
8
by: Steve Erickson | last post by:
I have a logger class that uses the Python logging module. When I call it within a program using the unittest module, I get one line in the log file for the first test, two identical ones for the...
6
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
2
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
23
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
3
by: Chris Smith | last post by:
Hola, pythonisas: The documentation for the logging module is good, but a bit obscure. In particular, there seems to be a lot of action at a distance. The fact that getLogger() can actually be a...
7
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
4
by: Frank Aune | last post by:
Hello, I've been playing with the python logging module lately, and I manage to log to both stderr and MySQL database. What I'm wondering, is if its possible to specify the database handler in...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.