[mod_python] http response code

Graham Dumpleton grahamd at dscpl.com.au
Tue Aug 22 04:27:55 EDT 2006


On 22/08/2006, at 5:25 PM, Ricardo Rocha wrote:

> Hi all.
>
> I'm not really sure if this is a mod_python or apache question, but  
> i'll give it a try anyway. My setup is Apache 2.0.55 with  
> mod_python 3.2.
>
> I've been looking for a way to send additional error messages along  
> with the HTTP error codes. The first thing i did was to remove the  
> default apache error messages by adding the following directives to  
> the apache config:
> ErrorDocument 400 " "
> ErrorDocument 401 " "
> ErrorDocument 404 " "
> ErrorDocument 406 " "
> ErrorDocument 409 " "
> ErrorDocument 500 " "
> ErrorDocument 501 " "
>
> Then i was hoping that returning the proper HTTP code from the  
> handler would do it... and it does, as long as i haven't written  
> anything back to the client before.
>
> If i do write something back inside the handler, if i try to return  
> apache.HTTP_BAD_REQUEST (for example), it will always give a 200  
> HTTP_OK code back to the client, along with the response text.
>
> Is there a way to overcome this and have both a response being  
> written back and a specific HTTP code being returned?

Use:

   def handler(req):
     req.status = apache.HTTP_BAD_REQUEST
     req.content_type = 'text/html'
     req.write(....)
     return apache.DONE

Setting req.status and req.content_type must be done before first  
write as
you found at.

Setting req.status and returning apache.DONE is key as that is what  
tells
Apache not to format its own error response.

Graham


More information about the Mod_python mailing list