[mod_python] Setting HTTP response status line

Graham Dumpleton graham.dumpleton at gmail.com
Sun May 6 17:53:38 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).

Where did you get the idea you could do that in the first place?

Where one argument is used with SERVER_RETURN it should be the handler
result code. Eg. apache.OK, apache.DONE or a specific error status
value such as apache.HTTP_BAD_REQUEST (400).

Where one argument is supplied and you return apache.OK or
apache.DONE, it is assumed you have overridden req.status yourself, if
not req.status is set to apache.HTTP_OK (200).

Where two argument are supplied, the first argument should be
apache.OK or apache.DONE. The second argument will be used to set the
value of req.status for you.

At no time can you supply an alternate status line message. Apache
will always use its own internal versions of the strings.

> The PythonDebug option is also confusing.  If I turn it on, then
> responses go back as 200 OK with the body of tracebacks etc, whereas
> with it off then you get 500 Internal Server Error with no traceback
> type information.

What is more confusing is why people keep using old versions of
mod_python where there is a newer, better, less buggy and more stable
version available. Upgrade to mod_python 3.3. See:

  http://issues.apache.org/jira/browse/MODPYTHON-167

> It seems to me that if you want to return anything other than success
> that Apache goes to great lengths to obliterate whatever it is that you
> were trying to send back and replaces it with its own canned
> information, hence the behaviour of PythonDebug.  Is there any way
> around this?

For the status line message the answer is no. Why would you want to
change the message from the normal one anyway?

If your complaint is because you are getting the mod_python traceback
error page when you use the above SERVER_RETURN, I'm not surprised as
your code is wrong and mod_python would be complaining that you are
giving it a string when it is expecting an integer.

Graham


More information about the Mod_python mailing list