[mod_python] i beg the developers to make mod_python can be import in python command line.

Richard Lewis richardlewis at fastmail.co.uk
Tue Aug 15 04:53:57 EDT 2006


On Tuesday 15 August 2006 05:35, guo tie wrote:
> hi,
>    All
>
>    These days i was work with mod_python, i found it is difficult to debug
> it , if you can made it be debuged in python command line mode ,
>
I do this:

In my module that I'm working on I start with this code:

======
try:
    from mod_python import apache
except:
    # if we're not running inside Apache (i.e. command line testing)
    # create a fake apache module
    class fake_apache:
        def __init__(self):
            self.HTTP_INTERNAL_SERVER_ERROR = 500
            self.HTTP_NOT_FOUND = 404
            self.OK = 200
            self.SERVER_RETURN = Exception
        def import_module(self, name):
            return __import__(name, globals(), locals())
        def server_root(self):
            return ""
    apache = fake_apache()
======

and then in a testing script I have something like this:
======
import my_module

class server:
    def __init__(self):
        self.cleanup_func = None
        self.server_hostname = ""

    def register_cleanup(self, req, func):
        self.cleanup_func = func

class request:
    def __init__(self, uri):
        self.uri = uri
        self.unparsed_uri = uri
        self._content_type = ""
        self.subprocess_env = 
{"SERVER_NAME": "localhost","DOCUMENT_ROOT": "/var/www"}
        self.server = server()
        self.server.server_hostname = self.subprocess_env['SERVER_NAME']

    def document_root(self):
        return self.subprocess_env['DOCUMENT_ROOT']
    
    def set_content_type(self, c):
        print >> sys.stderr, "Content-type: %s" % s
        self._content_type = c

    def get_content_type(self): return self._content_type

    content_type = property(fget=get_content_type, fset=set_content_type)
    
    def write(self, *args):
        for a in args:
            print a

    def sendfile(self, fn):
        f = open(fn, "r")
        print f.read()
        f.close()

    def set_content_length(self, l):
        print >> sys.stderr, "Content-length: %d" % l

    def add_common_vars(self):
        pass

r = request(sys.argv[1])

my_module.handler(r)

r.server.cleanup_func(r)
======

Which I run with a request URI as a parameter.

Cheers,
Richard
-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Richard Lewis
Sonic Arts Research Archive
http://www.sara.uea.ac.uk/
JID: ironchicken at jabber.earth.li
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-


More information about the Mod_python mailing list