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

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

    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

提交回复
热议问题