Created
June 4, 2013 08:42
-
-
Save madssj/5704549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def http_head(url): | |
import httplib | |
import urlparse | |
redirects = 0 | |
while redirects < 10: | |
scheme, netloc, path, query, fragment = urlparse.urlsplit(url) | |
if scheme == 'https': | |
conn = httplib.HTTPSConnection(netloc) | |
else: | |
conn = httplib.HTTPConnection(netloc) | |
conn.request("HEAD", "%s%s%s%s%s" % (path, query and "?" or "", query, | |
fragment and "#" or "", fragment)) | |
res = conn.getresponse() | |
if res.status in (301, 302) and res.getheader('location'): | |
url = res.getheader('location') | |
redirects += 1 | |
else: | |
break | |
return res.status, res.reason |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment