# problem with example.com def head_url(url): """Perform HEAD, may throw socket errors""" import httplib, urlparse def _head(url): """Returns a http response object""" host, path = urlparse.urlparse(url)[1:3] connection = httplib.HTTPConnection(host) connection.request("HEAD", path) return connection.getresponse() # redirection limit, default of 10 redirect = 10 # Perform HEAD resp = _head(url) # check for redirection while (resp.status >= 300) and (resp.status <= 399): # tick the redirect redirect -= 1 # if redirect is 0, we tried :-( if redirect == 0: # we hit our redirection limit, raise exception raise IOError, (0, "Hit redirection limit") # Perform HEAD url = resp.getheader('location') resp = _head(url) if resp.status >= 200 and resp.status <= 299: # horray! We found what we were looking for. return (resp.status, url, resp.reason) else: # Status unsure, might be, 404, 500, 401, 403, raise error # with actual status code. print "ERROR", (resp.status, url, resp.reason) raise IOError, (resp.status, url, resp.reason)