[mod_python] Mod Python Issue?

David Fraser davidf at sjsoft.com
Fri Feb 13 15:54:50 EST 2004

ntimm wrote:

>Are you saying import the module with pythonimport command?  Her is a
>piece of one of the scripts basically all scripts look like this except
>different query and results.   I have looked at sql relay  but just
>don't want to change all our code to support it.
>I have tried the pythonImport command but didn't know the correct
I don't think the PythonImport command is relevant here, your problem 
isn't with importing modules but connection caching...

>The varweb module contains a lot of the session handling.
How many database connections are you getting? I presume you are running 
this under Unix?
Are you aware that you get one connection for each Apache process? It 
may be that you have 20 or more processes, that is why you are seeing 
multiple connections


>import sys, shelve, random, pg, md5, time, string
>sys.path.insert(0, "/home/varlog/lib")
>from mod_python import apache
>#db = pg.connect('varlog','localhost')
>from varweb import *
>def default(req,sid):
>        sid = '%s'%(sid)
>        if check_session(sid) == 1:
>                orgid = '%s' %(org_id)
>                hid = '%s' %(host_id)
>                vquery = "select * from vuln_scan where o_id = %s and
>h_id = %s order by level desc limit 50;" %(orgid,hid)
>                output = vuln(sid,vquery)
>        else:
>                output = var_login_redirect()
>        return output
>def vuln(sid,query):
>        table = ['']
>        for data in db.query(query).dictresult():
>                Hid = data['h_id']
>                Oid = data['o_id']
>                Id = data['id']
>                Ip = data['ip']
>                Port = data['port']
>                Nid = data['nessus_id']
>                Adv = data['advisory']
>                Lev = data['level']
>                Date = data['time']
>                dat = '<a
>href="/text.py/default?sid=%s&id=%s">Details</a>' %(sid,Id)
>                False = data['is_false']
>                if False == 0:
>                        False = '<a
>href="/vulnerabilities.py/false?sid=%s&id=%s">No</a>' %(sid,Id)
>                else:
>                        False = 'Yes'
>                html =
>                table.append(html)
>        table_html = string.join(table,'\n')
>        output =
>        return output
>-----Original Message-----
>From: David Fraser [mailto:davidf at sjsoft.com] 
>Sent: Friday, February 13, 2004 12:47 AM
>To: ntimm
>Cc: mod_python at modpython.org
>Subject: Re: [mod_python] Mod Python Issue?
>ntimm wrote:
>>I am running mod python 3.0.4 with apache 2.0.47 connecting to a 
>>postgresql database. The only issue I am having is that each time a 
>>mod python script is called that has a db query in all just select 
>>statements if leaves a instance of postgres running and eventually my 
>>max connection to postgres is hit. I have tried the db.close 
>>statements but then that cause sporadic errors with the connection not
>>being valid. The only way I have found to close the postgres instance 
>>is either restart apache or postgres. I there some mod python variable
>>that I can use or any type of connection pooling Or maybe a apache 
>>config option I have tried messing with the apache config but with no 
>>I have tried to use PythonImport variable but couldn't get it to work 
>>with the pg module at all.
>>Any help would be appreciated.
>If you post some of the relevant code it will be easier to see what the
>problem is...
>But basically if you import the pg module, it will not be re-imported
>for each call.
>So you want to set up a global variable that is the connection, then
>just create a new cursor each time the script is run, and close the
>cursor at the end.
>Running under Unix, you will still get multiple connections because
>there are multiple Python Interpreters - one for each Apache process -
>but there will be a limit.
>Not sure about the connection pooling, but have a look at
>http://sqlrelay.sourceforge.net/ - I haven't used it, but it seems to be
>the kind of thing you want...

More information about the Mod_python mailing list