[mod_python] Setting HTTP response status line

Graham Dumpleton graham.dumpleton at gmail.com
Tue May 15 06:32:11 EDT 2007


On 07/05/07, Roger Binns <rogerb at rogerbinns.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I'm trying to set the response status line, and failing.
>
> def handler(req):
>    raise apache.SERVER_RETURN, (400, "You must supply a foo")
>
> This is with the mod_python on Ubuntu feisty (3.2.10).  From the code in
> apache.py, it sets req.status with the text message although the doc
> says that is an integer field.
>
> However the http client always receives standard text (eg 'Bad Request'
> for code 400).

It turns out that when using the Apache C API one can set the status
line just like you you want, unfortunately mod_python doesn't properly
expose this functionality. The only change required to make it work is
to make the req.status_line attribute writable. In doing that, it
overrides what status line would be calculated from req.status. Ie.,

  req.status_line = "400 You must supply a foo"
  req.write('some data')
  return apache.OK

I'll log an issue in the bug tracking system to have req.status_line
made writable in future version.

Graham


More information about the Mod_python mailing list