Python: Get HTTP headers from urllib2.urlopen call?

前端 未结 6 1115
猫巷女王i
猫巷女王i 2020-11-27 11:22

Does urllib2 fetch the whole page when a urlopen call is made?

I\'d like to just read the HTTP response header without getting the page.

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 11:35

    What about sending a HEAD request instead of a normal GET request. The following snipped (copied from a similar question) does exactly that.

    >>> import httplib
    >>> conn = httplib.HTTPConnection("www.google.com")
    >>> conn.request("HEAD", "/index.html")
    >>> res = conn.getresponse()
    >>> print res.status, res.reason
    200 OK
    >>> print res.getheaders()
    [('content-length', '0'), ('expires', '-1'), ('server', 'gws'), ('cache-control', 'private, max-age=0'), ('date', 'Sat, 20 Sep 2008 06:43:36 GMT'), ('content-type', 'text/html; charset=ISO-8859-1')]
    

提交回复
热议问题