Search code examples
pythonwebtwistedhttp-status-code-301http-redirect

Twisted: How to find the final destination in case of HTTP 301?


In case of a url that redirects with HTTP 301, twisted.web.client.getPage successfully emulates the browser's behaviour and returns a deferred that fires with the html / response_body of the final destination. How do I get the url of this destination page?


Solution

  • You can use twisted.web.client.Agent to perform a HEAD request in order to retrieve the Location header which is the URL you are looking for.

    See https://github.com/darkrho/Random-Code/blob/master/python/twisted/longurl.py

    The LocationResolver class performs a HEAD request and calls the callback with the value of the Location header. This only retrieve the first redirection, if you want to find the final URL you can follow what handleResult does, that is calling resolver again until you don't get a new location value.