How do you send a HEAD HTTP request in Python 2?

前端 未结 11 2097
甜味超标
甜味超标 2020-11-22 11:40

What I\'m trying to do here is get the headers of a given URL so I can determine the MIME type. I want to be able to see if http://somedomain/foo/ will return a

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 12:01

    import httplib
    import urlparse
    
    def unshorten_url(url):
        parsed = urlparse.urlparse(url)
        h = httplib.HTTPConnection(parsed.netloc)
        h.request('HEAD', parsed.path)
        response = h.getresponse()
        if response.status/100 == 3 and response.getheader('Location'):
            return response.getheader('Location')
        else:
            return url
    

提交回复
热议问题