list+mod_python at joreybump.com
Sat Apr 10 20:33:01 EST 2004
John Draper wrote: > >4. Edit mptest.py file in the htdocs/test directory so that is has > the following lines (be careful when cutting and >pasting from your > browser, you may end up with incorrect indentation and a syntax error): > > > from mod_python import apache > > > def handler(req): > > req.write("Hello World!") > > return apache.OK > > Ok - I've done that, and saved it into cgi-bin/test directory. Don't do that, it's not CGI. > >5. Point your browser to the URL referring to the mptest.py; you > should see "Hello World!". If you didn't - refer to the troubleshooting > section next. > > Ok, I tried: > > http://192.168.0.20/mptest - Not found > http://192.168.0.20/mptest.py - Not found > http://192.168.0.20/cgi-bin/test/mptest - Not found > http://192.168.0.20/cgi-bin/test/mptest.py - Not found > > Ok, so what IS the url if it isn't any one of these..... COME ON > MAN.... this documentation sucks > it says "Point your browser to the URL referring to the mptest.py" but > fails to specify what the URL > really IS.... So how are WE to know this? There is no universal apache directory layout. Only you know the valid paths for your web site files. Try an easier test: Open httpd.conf (wherever you keep it). Look for your DocumentRoot. It might look something like this: DocumentRoot "/var/www/html" That means that your homepage would be /var/www/html/index.html, or something similar. Add this to httpd.conf, but use your own DocumentRoot: <Directory /var/www/html> AddHandler python-program .py PythonHandler mod_python.publisher PythonDebug On </Directory> Save httpd.conf, and move to your DocumentRoot: cd /var/www/html Create a file called example.py in your favorite text editor. Put only these lines in it: def hello(req): x = "Hello, world!" return x Restart apache. Point your browser at: http://192.168.0.20/example.py/hello Note that with this configuration, you can put somefile.py almost anywhere in your site, and mod_python will handle it. There are other gotchas, as well, and I'm sure you'll encounter them. To avoid some of the pitfalls, I recommend that you strive to name your functions and variables uniquely (they share an interpreter), and simply import entire modules (don't use "from somemodule import something"). This means you'll have to use somemodule.somefunction() syntax, but it will protect you from errors that don't occur as readily with CGI. More experienced pythonistas are better able to explain the pros/cons of this, and provide other tips to avoid name clashes under mod_python. Good luck!