Problems with setting up mod_python and Apache

J

John Dean

Hi
I have set up httpd.conf according to installation and configuration
instructions in the mod_python documentation. Yet when I enter the following
URL http://localhost/test.py all I get is an Apache view of my htdocs
directory. They if I click on test.py and open file dialog pops up. If I
give the file a cgi extension and enter the following URL
http://locathost/test.cgi it runs fine. I just can't figure out what I am
doing wrong.
 
D

Dave Benjamin

John Dean said:
I have set up httpd.conf according to installation and configuration
instructions in the mod_python documentation. Yet when I enter the following
URL http://localhost/test.py all I get is an Apache view of my htdocs
directory. They if I click on test.py and open file dialog pops up. If I
give the file a cgi extension and enter the following URL
http://locathost/test.cgi it runs fine. I just can't figure out what I am
doing wrong.

Sounds like a configuration problem. Can you post the relevant lines from
your httpd.conf file? Are you trying to write handlers or are you using the
publisher module?

Dave
 
D

Dave Benjamin

Quoting John Dean:
Here are the lines in my httpd.conf
LoadModule python_module /usr/lib/apache/mod_python.so

AddModule mod_python.c

<Directory "srv/www/htdocs">
.....
.....

Addhandler python-program .py
PythonHandler test

Looks right to me. Have you verified that:
a) you are editing the right httpd.conf (hey, it happens...)
b) you are affecting the directory you think you are (do other apache
directives work properly?)

Once things are working, you may also want to read up on and consider adding
this:
PythonInterpPerDirectory on
(hint: otherwise, if you have a test.py in another directory, you will get
bizarre results)

Good luck,
Dave

PS - Please keep replies to the list for the benefit of others. =)
 
J

John Dean

Hi Dave

I have fixed my problem with mod_python. I have one small problem left to
sort out. If I enter the URL http://localhost Apache just displays the
contents of the htdocs directory, but if I enter the URL
http://localhost/test.py everything works as it should. I guess this is
somewhat off topic since it appears to be an Apache configuration problem so
I hope you don't mind me asking you for your opinion on c.l.p.

BTW What I am trying to do is to move away from PHP. Since I use Python as
my primary scripting language for just about everything where you would use
a scripting language, it seems to me to be a reasonable idea to use Python
to provide dyanamic content for the web sites I look after.
 
S

Steve Holden

John Dean said:
Hi Dave

I have fixed my problem with mod_python. I have one small problem left to
sort out. If I enter the URL http://localhost Apache just displays the
contents of the htdocs directory, but if I enter the URL
http://localhost/test.py everything works as it should. I guess this is
somewhat off topic since it appears to be an Apache configuration problem so
I hope you don't mind me asking you for your opinion on c.l.p.
Yes, you should condition your server not to give you a directory listing
when the default documents aren't found - this is big-time information
leakage, and gives an attacker leverage to start finding vulnerabilities.
So, beofre you go into production, fix that! The config command you need is
something like

DirectoryIndex index.htm index.html index.html.var index.php

You might want to include index.py there too!

How did you solve your mod_python problem, by the way?
BTW What I am trying to do is to move away from PHP. Since I use Python as
my primary scripting language for just about everything where you would use
a scripting language, it seems to me to be a reasonable idea to use Python
to provide dyanamic content for the web sites I look after.
Nice. Just try to resist the temptation to replace working PHP with working
Python - nobody's paying you for that! Translate to Python when upgrading,
or for new functionality.

still-from-Yorkshire-ly y'rs - steve
 
J

John Dean

Hi Steve
Thank you very much for your reply
Funnily enough I sorted out Apache problem before reading you post. Thanks
all the same.
I have mod_python working like a dream. So now in to the hard part. I hope
you won't mind if I explain. The project I am working on, Rekall, contains
serveral WSYWIG designers. Two of these designers are of particular interest
for what I have in mind - the Form and Report Designers. Form and Report
definitions are stored in XML format either as a file in the file system or
in a SQL Database. The basic idea is to load a string object with the form
or report definition. Then using Pythons XML DOM module build a HTML
document plus the required SQL query string(s). If it is a form then we'll
will need to build both the HTML data input form and the backend form
processor. I guess this will need a publisher handler. I expect Reports to
be a lot easier, but since I am talking about several report output during a
single session I suppose I will need to get my head around dynamic handlers.
Now that's frightening
 
D

Dave Benjamin

John Dean said:
I have fixed my problem with mod_python. I have one small problem left to
sort out. If I enter the URL http://localhost Apache just displays the
contents of the htdocs directory, but if I enter the URL
http://localhost/test.py everything works as it should. I guess this is
somewhat off topic since it appears to be an Apache configuration problem so
I hope you don't mind me asking you for your opinion on c.l.p.

Well, I get around this problem by adding "index.py" to the DirectoryIndex
directive. I use a modified version of the publisher module that comes with
mod_python that handles this and related special cases. Here's the change
that I make, if you're interested:

< if not _req.subprocess_env.has_key("PATH_INFO"):
< raise apache.SERVER_RETURN, apache.HTTP_NOT_FOUND
<
< func_path = _req.subprocess_env["PATH_INFO"][1:] # skip fist /
< func_path = string.replace(func_path, "/", ".")
---
if _req.subprocess_env.has_key("PATH_INFO"):
func_path = _req.subprocess_env["PATH_INFO"][1:] # skip first /
func_path = string.replace(func_path, "/", ".")

# If no function is specified, assume "default".
if not len(func_path): func_path = "default"
else:
# If no module is specified either, assume "index".
func_path = "default"
module_name = "index"

This has the result that "index" is always the default module, and "default"
is the function within that module that gets called if no function is
explicitly specified on the URL. This works like a charm.
BTW What I am trying to do is to move away from PHP. Since I use Python as
my primary scripting language for just about everything where you would use
a scripting language, it seems to me to be a reasonable idea to use Python
to provide dyanamic content for the web sites I look after.

I did the same thing myself last year, after writing PHP for a couple of
years. mod_python definitely makes Python a worthy substitute for writing
Apache modules. The main drawbacks from my experience are the lack of
built-in session handling and more sophisticated parameter passing (ie.
PHP's ability to serialize hashes from a form submission). But I'm a little
dated now, I'm still using mod_python 2 / apache 1.3 ...

Have fun,
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top