[mod_python] transhandler setup?

IR labs labs at ixopusada.com
Thu Sep 1 20:54:43 EDT 2005


Hi,

I am not sure whether I should start a new thread, but my issues are 
not yet solved and the subject line is still partly accurate, so I'll 
just continue.

To get my ideas working, I tried both the options that I saw in my last 
post, but I am getting stuck in both.

Let me start with the one, with which I progressed the most:
1. Don't use transhandler. Have a main.py as the PythonHandler for the 
www/root directory.

<Directory /var/www/root>
     SetHandler mod_python
     PythonHandler main
</Directory>

---- with main.py: ----
def handler(req):
	extension = os.path.splitext(req.filename)[1]
	if extension == ".html" or extension == "":
		if not os.path.exists(req.filename):
			... (handle further)
			return apache.OK
		else:
			if extension == '':
				req.filename = req.filename + "/index.html" # THIS LINE SEEMS NOT 
TO HAVE AN EFFECT
			return apache.DECLINED
	else:
		return apache.DECLINED

When a url is requested for an existent .html file, it works fine: 
apache serves the .html file itself. However if the url is only the 
name of a directory, I can't seem to give apache its normal –complete– 
behavior. (E.g. the url www.foobar.com/foo/ (assuming the directory 
foo/ exists) does not return a directory listing of the contents of 
that directory nor does it display www.foobar.com/foo/index.html if 
there is such a file.) And it seems I also can't change the filename 
anymore in that handler phase. The only solution I found was to put an 
.htaccess file in /foo/ saying "SetHandler none". But I was actually 
looking for a way in which I would not need these .htaccess files when 
there is just an index.html present. Is there any way I still can set 
the filename in this phase?


Then I thought that maybe transhandler could help me in the end, but I 
got stuck with that one rather soon.
2. Using transhandler to set the filename and have it decide what 
apache should be doing.

<IfModule mod_python.c>
     PythonPath "sys.path + ['/var/www/python/']"
     PythonTransHandler translate
</IfModule>

---- and from translate.py: ----

def transhandler(req):
	...
	req.filename = '/var/www/python/main.py'
	return apache.OK

The issue I run against is that now apache shows the code from main.py. 
It doesn't execute it, it just shows the code. Despite all the other 
directives I use (in httpd.conf or .htaccess). Even when I add the line 
'req.add_handler("PythonHandler", "main")' to the mix, it won't execute 
main.py, just displays its code. Any ideas how to I solve this?

Many thanks,
dirk


On 31-aug-05, at 00:49, Graham Dumpleton wrote:

>
> On 31/08/2005, at 5:44 AM, IR labs wrote:
>>
>> But thanks for this solution to have everything (also the stuff we 
>> don't want to handle) handled by the handler in main.py.
>> My only remaining question now, would be what you recommend?
>> 1. Don't use the transhandle phase, and have *all* requests handled 
>> by the main PythonHandler, including the ones it shouldn't handle ( 
>> apache.DECLINE)
>> or.
>> 2. Use the TransHandler phase to discern between real physical 
>> present files (requests for .html, .css, .js etc. files) and virtual 
>> pages that should be handled by main.py
>
> It all depends on exactly what you want to do, as these probably aren't
> the only alternatives. If you want a request against a directory to be
> rerouted, look at the DirectoryIndex directive. If you want to reroute 
> a
> request against a non existent file based on a specific extension type,
> you may also be able to use mod_rewrite. Finally, there is also the
> ErrorDocument directive for non existent files as well. All of these
> might feasibly be harnessed to provide part of the solution and using
> Python to make the decision may be a more complicated choice than some
> of Apache's core features designed for this sort of thing.
>
> Graham
>
>
>

-----------------------------
Dirk van Oosterbosch
dirk at ixopusada.com
-----------------------------





More information about the Mod_python mailing list