Graham Dumpleton
grahamd at dscpl.com.au
Tue Jan 16 05:31:52 EST 2007
On 16/01/2007, at 9:07 PM, Maksim Kasimov wrote: > > > On 1/15/07, Graham Dumpleton <grahamd at dscpl.com.au> wrote: > Maksim Kasimov wrote .. > > hi, > > > > in apache conf file i use: > > > > <Directory "/mypath/mydir"> > > SetHandler python-program > > PythonHandler mod_python.publisher > > </Directory> > > > > > > there is file: > > > > in file /mypath/mydir/index.py there some functions: > > > > def index(req): > > > > return "index" > > > > > > def free(req): > > > > return 'free' > > > > > > def not_found(req): > > > > # some computation > > return 'page not found' > > > > > > > > and every thing is just fine and works as expected: > > > > for http://myhostname.org/mydir/free i can see "free" > > for http://myhostname.org/mydir/ i can see "index" > > > > > > my question is: How to get output of "not_found" function for any > other > > uri, > > such as " http://myhostname.org/mydir/hello" ? > > > > many thanks for any advice. > > Use the Apache ErrorDocument directive, specifying the URL to the > not_found > function as the argument. > > Graham > > it is very inconveniently because: > 1) in the directory "mydir" there can be a lot of *.py scripts > (not only index.py), and i need to specify some default handler for > each of module in the directory. > 2) it makes superfluous redirections But if it is only for not found errors, it wouldn't occur much so why would it matter. > So my question is still opened. > > To provide this, temporary, i've changed resolve_object function in > publisher.py from mod_python in that way: > > try: > #obj = getattr(obj, obj_str) > obj = getattr(obj, obj_str, getattr(obj, '_default')) > except AttributeError: > raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND > > than, i will define _default function in my /mypath/mydir/index.py > or any other modules in mydir. > > i think it is very wrong to change standart libraries, but i did > not found the way, how to redifine this behavior in my index.py file. > > is it possible to define some function in my module, to make it > return default attribute for getattr function? > or may be, if i dare, to expect such changes as above in future > releases of mod_python? It is unlikely you will see such a feature in future versions of mod_python. I also question why you would want to do that anyway, ie., have a separate not found page for every file resource. Care to explain what you thing you get from it? Maybe you shouldn't be using mod_python.publisher but should be writing a custom dispatcher of your own. BTW, you could always do the following: SetHandler mypublisherwrapper Then in handler() of mypublisherwrapper do something like (untested): from mod_python import apache, publisher def handler(req): try: result = publisher.handler(req) except SERVER_RETURN, value: try: if len(value.args) == 2: raise else: result = value.args[0] if result == apache.HTTP_NOT_FOUND: req.filename = req.filename + '/err404' req.path_info = '' result = publisher.handler(req) return result although I wouldn't recommend it. Anyway, please explain why as there is probably a better way of doing it but not knowing what you are trying to achieve makes it very hard to suggest anything. Graham -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20070116/1a2bf2e7/attachment.html
|