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

前端 未结 11 1994
甜味超标
甜味超标 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 11:56

    urllib2 can be used to perform a HEAD request. This is a little nicer than using httplib since urllib2 parses the URL for you instead of requiring you to split the URL into host name and path.

    >>> import urllib2
    >>> class HeadRequest(urllib2.Request):
    ...     def get_method(self):
    ...         return "HEAD"
    ... 
    >>> response = urllib2.urlopen(HeadRequest("http://google.com/index.html"))
    

    Headers are available via response.info() as before. Interestingly, you can find the URL that you were redirected to:

    >>> print response.geturl()
    http://www.google.com.au/index.html
    
    0 讨论(0)
  • 2020-11-22 12:01

    Just:

    import urllib2
    request = urllib2.Request('http://localhost:8080')
    request.get_method = lambda : 'HEAD'
    
    response = urllib2.urlopen(request)
    response.info().gettype()
    

    Edit: I've just came to realize there is httplib2 :D

    import httplib2
    h = httplib2.Http()
    resp = h.request("http://www.google.com", 'HEAD')
    assert resp[0]['status'] == 200
    assert resp[0]['content-type'] == 'text/html'
    ...
    

    link text

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-22 12:06

    Obligatory Requests way:

    import requests
    
    resp = requests.head("http://www.google.com")
    print resp.status_code, resp.text, resp.headers
    
    0 讨论(0)
  • 2020-11-22 12:08

    For completeness to have a Python3 answer equivalent to the accepted answer using httplib.

    It is basically the same code just that the library isn't called httplib anymore but http.client

    from http.client import HTTPConnection
    
    conn = HTTPConnection('www.google.com')
    conn.request('HEAD', '/index.html')
    res = conn.getresponse()
    
    print(res.status, res.reason)
    
    0 讨论(0)
提交回复
热议问题