[mod_python] mptest.py works, other .py files don't

Nicolas Lehuen nicolas.lehuen at gmail.com
Wed Jan 19 19:03:53 EST 2005


On Thu, 20 Jan 2005 00:39:01 +0100, Bas van Dorp <bas at vandorp.nu> wrote:
[...]
> Guess what? This works :-)) However, when I rename this file to
> mptest2.py, I get this:
> 
> Mod_python error: "PythonHandler mptest"
> 
> Traceback (most recent call last):
> 
>   File "/usr/local/lib/python2.3/site-packages/mod_python/apache.py", line 287, in HandlerDispatch
>     log=debug)
> 
>   File "/usr/local/lib/python2.3/site-packages/mod_python/apache.py", line 454, in import_module
>     f, p, d = imp.find_module(parts[i], path)
> 
> ImportError: No module named mptest
> 
> This was not what I expected. I've read this over and over:
> http://www.modpython.org/live/current/doc-html/tut-what-it-do.html
> 
> The last paragraph explicitly states that the name of my python script
> doesn't matter. I probably missed something... I've tried a lot of
> things, but I still get errors. I seem to be able to make mod_python to
> execute only the scripts I specify in httpd.conf, and nothing else.
> Specifying multiple files in httpd.conf also gives interesting results.
> For example:
> 
> ==> httpd.conf:
> <Directory /home/www/basd/python>
>     AddHandler mod_python .py
>     PythonHandler mptest
>     PythonHandler mptest2
>     PythonDebug On
> </Directory>
> 
> ==> mptest.py:
> from mod_python import apache
> 
> def handler(req):
>     req.write("Hello World!")
>     return apache.OK
> 
> ==> mptest2.py:
> from mod_python import apache
> 
> def handler(req):
>     req.write("Hello Universe!")
>     return apache.OK
> 
> Whether I point my browser to mptest.py or mptest2.py, I get the same
> result:
> 
> Hello World!Hello Universe!
> 
> I don't get it. I would like to tell Apache to parse all .py scripts in
> certain directories, just like I do with .php scripts. But how?
> 
> Greetings and thanks in advance,
> 
> Bas

The name of the .py file in the URL doesn't matters, but the name of
the PythonHandler does. I think we should change the doc because
you're not the first to be confused about this.

The configuration file reads like this :

# for directory /home/www/basd/python
# which is accessed through something like http://localhost/basd/python
<Directory /home/www/basd/python>
     # any filename ending by .py must be handled by mod_python
     AddHandler mod_python .py
     # mod_python will call the handler function defined in the mptest module
     # which will be imported from the /home/www/basd/python
directory, hence from
     # /home/www/basd/python/mptest.py
     PythonHandler mptest
     # add debug information i.e. outputs stack traces to the client
     PythonDebug On
 </Directory>

Now I hope it's clear : with this configuration, the module with then
handler() function MUST be in /home/www/basd/python/mptest.py. But you
can test the module by requesting ANY URL provided it ends with .py :

http://localhost/basd/python/foo.py
http://localhost/basd/python/bar.py

And so on. Naturally, this test handler won't lead you very far... To
build your real application, you'll have to write some Python or PSP
code for each and every page or function your site provide.

What you need, therefore, is a dispatching handler function, which
knows where to find the code for the various pages of your
application. That's what you get with the builtin mod_python.publisher
handler (which is described in the documentation), or other handlers,
like the one provided by spin-off projects like mod_python servlets
(MPS), Vampire, etc.

I hope this helps,
Regards,
Nicolas


More information about the Mod_python mailing list