Search code examples
pythonhttp-redirecthttp-headerslocationtwisted

twisted location header redirect


From the render_GET method of a Resource in twisted, is it possible to redirect to a different url entirely (hosted elsewhere)

request.redirect(url) doesn't appear to do anything, and neither does twisted.web.util.Redirect

The equivalent in php would be,

header('location:'.$url);

EDIT

this is the code I'm running

from twisted.web import server, resource
from twisted.internet import reactor

class Simple(resource.Resource):
    isLeaf = True
    def render_GET(self, request):
        request.redirect("www.google.com")
        request.finish()

site = server.Site(Simple())
reactor.listenTCP(8080, site)
reactor.run()

Solution

  • I worked it out in the end with help from the other poster, with request.finish(), the redirect including http:// and returning NOT_DONE_YET

    from twisted.web import server, resource
    from twisted.internet import reactor
    
    class Simple(resource.Resource):
        isLeaf = True
        def render_GET(self, request):
            request.redirect("http://www.google.com")
            request.finish()
            return server.NOT_DONE_YET
    
    site = server.Site(Simple())
    reactor.listenTCP(8080, site)
    reactor.run()