[mod_python] mod_python article

Graham Dumpleton grahamd at dscpl.com.au
Wed Nov 9 17:01:29 EST 2005

A few comments of my own to expand on what Jorey as already
pointed out.

Jorey Bump wrote ..
> Joshua D. Drake wrote:
> > Hello,
> > 
> > I wrote a brief introduction to mod_python/psp over at DevX about a 
> > month ago. I am by no means an expert Python guy but I thought it may
> be
> > useful for people new to mod_python. The article can be found here and
> I 
> > hope it is helpful.
> > 
> > http://www.devx.com/webdev/Article/29357
> I see a few problems with your Publisher example:
> 1. Replace AddHandler with SetHandler in your example configuration.

Even if SetHandler is used, with the action specified in the form, the
mod_python code would need to be saved as "python/index.py".

If the name of the mod_python code file were "python/getmonth.py" then
your action would need to be "python/getmonth.py/getMonth".

It isn't clear what the name of the mod_python code file is supposed to
be, so anyone following the article may get confused.

One could still use AddHandler if it were corrected to:

  AddHandler mod_python.py

but, presuming that the mod_python code is in "python/getmonth.py"
you would also still need to change the action of the form to be

If you want to not be able to use ".py" in the URL, then you have extra
work to do to ensure that MultiViews matching in Apache is properly

> 2. Provide the name of your published module.

Is this what I described further above as far as the name of the mod_python
code file, or do you mean something else???

> 3. Use a return statement instead of req.write().

Reason for this is that if req.write() is used, your "Authors Note"
about not needing to send content type is wrong. If you use req.write()
you must set the content type. A content type is only set for you when
using mod_python.publisher when the content is returned from the
function as the result and no use of req.write() has occurred.

Even if content type were set appropriately and req.write() were used
there is an issue in mod_python.publisher <= 3.1.4 whereby not
returning anything from the function (ie., None), or even an empty
string is going to result in a HTTP 500 error being raised. As a hack
return a string with a newline in it, ie., "\n".

Thus you mod_python code becomes:

   import calendar
   def getMonth(req,month):
      req.content_type = 'text/plain'
      req.write(calendar.month(2005, int(month),2,3))
      return "\n"

or if returned as result:

  import calendar
  def getMonth(req,month):
      return calendar.month(2005, int(month),2,3)

Suggest you drop the "#!/usr/bin/python" from the mod_python'd
example as not necessary.


More information about the Mod_python mailing list