[mod_python] how to define publisher when page not found

Maksim Kasimov maksim.kasimov at gmail.com
Tue Jan 16 07:14:44 EST 2007


On 1/16/07, Graham Dumpleton <grahamd at dscpl.com.au> wrote:
>
>
> 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
> butnot knowing what you are trying to achieve makes it very hard to
> suggest
> anything.
> Graham
>

Hi Graham, i'm very pleased to get your answer, thank you!

>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?

take it as "default attribute", not as "not found page", but defining
default attribute is a native thing (at least for pythonic applications).
And having ability to define or not define default attribute, in case of
mod_python.publisher, is also not a bad thing, just make it more flexible.
Isn't it?
But any way, i do not  insist.

As we all know, there not only web related projects that uses Apache
web-server and can use mod_python.publisher - there are a lot of things
based on http-protocol.
For example i'm planning to port my Wireles Village project based on
pythonic web-server to Apache, and there many of such things that exists at
the moment and can appears in future.

For web case: all users of my site are mobile operators subscribers, they
download various content from my site (melodies, pictures and so on). For a
different mobile operators, there are different requirements: page design,
error messages format, error message text and many other parameters, it can
be wml, xhtml or html format ... and some times some operator can change its
own requirements (as i can see it happens many times) and so on.
in my turn, i can have a lot of related projects, they must have different
logic, or just a design or use different technology to download (such as
drm-technology)

For various project it is convinient to have different directories, and i
can make some modules with names related to some operator.

One project can have different parts: web/wap-based part, and some
http-based part (Wireless Village + gate to another messengers, SyncML or
smth else)

My case is not unique.


>Maybe you shouldn't be using mod_python.publisher but should
>be writing a custom dispatcher of your own.

it is looks like a discovering a bicycle again and again. But exactly that
change i've suggest before, do not make some unexpected things for those
developers that uses mod_python.publisher but did not define _default
function.



-- 
Maksim Kasimov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20070116/da4297ae/attachment-0001.html


More information about the Mod_python mailing list