{"id":55,"date":"2010-05-08T19:47:48","date_gmt":"2010-05-09T02:47:48","guid":{"rendered":"https:\/\/www.zacwitte.com\/?p=55"},"modified":"2010-05-08T19:47:48","modified_gmt":"2010-05-09T02:47:48","slug":"resolving-http-redirects-in-python","status":"publish","type":"post","link":"https:\/\/zacwitte.com\/?p=55","title":{"rendered":"Resolving HTTP Redirects in Python"},"content":{"rendered":"<p>Since everyone is using short urls these days and sometimes we just need to know where that URL leads I wrote this handy little function which finds out for us. Redirection can be a kind of tricky thing. We have 301 (&#8220;permanent&#8221;) and 302 (&#8220;temporary&#8221;) style status codes and multiple layers of redirection. I think the simplest approach to take is whenever the server returns a Location http header and the value in that location field is not the same as what you made the request to, we can pretty well be sure that it&#8217;s a redirect. The function below uses the http HEAD verb\/method to request only the headers so as not to waste bandwidth and recursively calls itself until it gets a non-redirecting result. As a safeguard against infinite recursion I have a depth counter.<\/p>\n<pre>\nimport urlparse\nimport httplib\n\n# Recursively follow redirects until there isn't a location header\ndef resolve_http_redirect(url, depth=0):\n    if depth > 10:\n        raise Exception(\"Redirected \"+depth+\" times, giving up.\")\n    o = urlparse.urlparse(url,allow_fragments=True)\n    conn = httplib.HTTPConnection(o.netloc)\n    path = o.path\n    if o.query:\n        path +='?'+o.query\n    conn.request(\"HEAD\", path)\n    res = conn.getresponse()\n    headers = dict(res.getheaders())\n    if headers.has_key('location') and headers['location'] != url:\n        return resolve_http_redirect(headers['location'], depth+1)\n    else:\n        return url\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Since everyone is using short urls these days and sometimes we just need to know where that URL leads I wrote this handy little function which finds out for us. Redirection can be a kind of tricky thing. We have 301 (&#8220;permanent&#8221;) and 302 (&#8220;temporary&#8221;) style status codes and multiple layers of redirection. I think [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[20],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-tech","tag-python"],"_links":{"self":[{"href":"https:\/\/zacwitte.com\/index.php?rest_route=\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zacwitte.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zacwitte.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zacwitte.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zacwitte.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=55"}],"version-history":[{"count":0,"href":"https:\/\/zacwitte.com\/index.php?rest_route=\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/zacwitte.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zacwitte.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zacwitte.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}