[mod_python] Getting request object

Radek Bartoň blackhex at post.cz
Wed Dec 7 03:21:34 EST 2005

Hello, thanks for your reply.

> If what it is that you want is that an instance of MyClass be created
> specific for each request, before any URL mapping against methods is
> being done, then using mod_python.publisher alone the answer is that no
> it cannot be done easily. Unless my brain is overlooking something
> obvious, you have to do something convoluted like:
> from mod_python import util, apache
> import types
> class Wrapper:
>   def __init__(self,function):
>     self.__class = function.im_class
>     self.__name = function.im_func.__name__
>   def __call__(self,req):
>     instance = self.__class(req)
>     function = getattr(instance,self.__name)
>     return util.apply_fs_data(function,req.form)
> class MyClass:
>   def __init__(self,req):
>     self.__req = req
>   def method1(self):
>     return "method1"
>   def method2(self):
>     return "method2"
> class Object: pass
> index = Object()
> index.method1 = Wrapper(MyClass.method1)
> index.method2 = Wrapper(MyClass.method2)

 I tried this wrapper. When I typed URL http://localhost/test.py/index page 
returned string representation of object so I could write __str__ function of 
Object class to make it working. But URLs  
http://localhost/test.py/index/method1 and  
http://localhost/test.py/index/method2 returned errors "Not Found".
  Nevertheless I think that functionality of this wrapper was mentioned same 
as functionality of my first example:

class MyClass:
   def __init__(self):
   def __str__(self):
   def method1(self, req):
   def method2(self, req):

index = MyClass():

I don't know much about mod_python and its publisher internal function but 
this one works because I think that publisher when parse request URL looks 
first for matching file in web root and then it looks into this file for 
index function or for index object. If index object has defined __str__ 
function publisher returns string representation of this object as reply for 
request. If index object has methods and URL is containing identifier of some 
method publisher call it. Repair me if this is bad idea.

> Apart from that, if you were using Vampire, you could simply use basic
> handlers are write:
> import vampire
> class MyClass:
>   def __init__(self,req):
>     self.__req = req
>   def __call__(self):
>     return "__call__"
>   def method1(self):
>     return "method1",self.__req.filename
>   def method2(self, req):
>     return "method2",req.filename
> handler = vampire.Publisher(MyClass)
> The vampire.Publisher() wrapper class handlers both creating an instance
> of the class and mapping URLs onto methods of the instance. It only
> supports callable methods in classes though. Ie., it will not map URLs
> to __str__() or to basic data types which are a member of the class.

This looks that it cold have functionality I want. But mayby it will be 
sufficent to look how vampire gets request object and write this to MyClass's 

> Note that creating an instance of a class per request could be quite
> inefficient. What is the original reason that you wanted to access the
> request object in the constructor in the first place?

I didn' know that when I use 

def index(req):
   instance = MyClass(req)

mod_python creates only one instance of MyClass for every request and when I 

index = MyClass

it creates instance for each. But creating instance isn't the purpose. Purpose 
is to have page witch is class. Its string representation or its callable 
function returns main content with forms i. e. and its methost handles 
request connected with forms on main page.

Hope that I make my self clear and thanks for response.


More information about the Mod_python mailing list