[Twisted-web] Returning an HTTP error from render method

Grant Baillie grant at osafoundation.org
Wed Jun 22 11:11:57 MDT 2005


You could do something like

import twisted.web.error as error


def render_GET(self, request):
   if (request.path != '/hello'):
     page = error.NoResource(message="The resource %s was not found"  
% request.URLPath())
     return page.render(request)

    ...

This will set the 404 response, and the return value of render() is  
some html including your message. There are other handy error pages  
in twisted.web.error.

--Grant

which will set the 404, and include a relat

On Jun 22, 2005, at 9:13 , Jonathan Blocksom wrote:

> Hi, I'm trying to figure out how to return an HTTP error in the  
> render method of a custom Resource.  I can't find any docs on it,  
> but from poking around in the code I came up with the code below  
> that doesn't do it.  Can somebody point me in the right direction  
> for returning an error from the render method?  Or should I be  
> returning the error somewhere else?
>
> Thanks.
>
> ---
>
> from twisted.web import server
> from twisted.internet import reactor
> from twisted.web.resource import Resource
>
> class Sample404(Resource):
>     "Returns a 404 error for everything except /hello"
>
>     isLeaf = True
>     def render_GET(self, request):
>         if (request.path != '/hello'):
>             request.setResponseCode(404)
>             request.finish()
>             return server.NOT_DONE_YET
>         else:
>             return "<html>You found me!</html>"
>
>
> if __name__=='__main__':
>     site = server.Site(Sample404())
>
>     reactor.listenTCP(8080, site)
>     reactor.run()
>
>
>
> _______________________________________________
> Twisted-web mailing list
> Twisted-web at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
>
>




More information about the Twisted-web mailing list