If you're using the urlfetch API, you can just access the final_url
attribute of the response object you get from urlfetch.fetch()
, assuming you set follow_redirects
to True
:
>>> from google.appengine.api import urlfetch
>>> url_that_redirects = 'http://www.example.com/redirect/'
>>> resp = urlfetch.fetch(url=url_that_redirects, follow_redirects=False)
>>> resp.status_code
302 # or 301 or whatever
>>> resp = urlfetch.fetch(url=url_that_redirects, follow_redirects=True)
>>> resp.status_code
200
>>> resp.final_url
'http://www.example.com/final_url/'
Note that the follow_redirects
keyword argument defaults to True
, so you don't have to set it explicitly.