[mod_python] setting content_type intelligently

Daniel J. Popowich dpopowich at mtrsd.k12.ma.us
Tue Jan 13 10:40:53 EST 2004


In most of my mod_python apps if a request comes in for a file that I
don't want to handle, let's say an image file, foo.gif, I raise
apache.SERVER_RETURN with a value of apache.DECLINED.  Apache then
handles the request and sends in the header:

  Content-Type: image/gif

However, I now have an app that wants to live in a <Location ...>
directive so returning apache.DECLINED doesn't work: apache has no
other means of handling the request so it returns a 404 error.  My
only means of recourse seems to be something like this:

    if NOT_HANDLING_PAGE:
        req.write(open(filename).read())

but content_type is not set properly (it appears to default to
text/plain), so binary files, like image files, appear as gibberish in
a browser.

Two questions:

   1.  What does mod_python/apache do if content_type is not set by a
       handler?  Does it default to text/plain?

   2.  I'm looking for an intelligent means of setting the
       content_type.  I know I can use mimetypes.guess_type(), but
       that is based only on the file's extension.  Apache does more
       than that, eg, if 'foo.gif' was named 'foo' apache still
       determines the content type correctly as image/gif.  Is there a
       way to tap into apache's content_type determination?

Thanks,

------------------
Daniel Popowich
Network Specialist
-------------------------------------
Mohawk Trail Regional School District
24 Ashfield Rd.
Shelburne Falls, MA 01370
413.625.0192 x22
-------------------------------------



More information about the Mod_python mailing list