【问题标题】:HEAD request in python not working as desiredpython中的HEAD请求无法按预期工作
【发布时间】:2012-09-26 14:18:46
【问题描述】:

我正在尝试使用以下代码检查 Python 中任何 URL 的状态代码

class HeadRequest(urllib2.Request):
    def get_method(self):
        return "HEAD"

当我这样使用它时:

response = urllib2.urlopen(HeadRequest("http://www.nativeseeds.org/"))

它抛出以下异常:

HTTPError: HTTP Error 503: Service Temporarily Unavailable

但是,当我在 firefox/restclient 中打开上述 URL“http://www.nativeseeds.org/”时,它会返回 200 状态码。

我们将不胜感激。

【问题讨论】:

    标签: python http-headers urllib2


    【解决方案1】:

    经过一番调查,网站要求同时设置AcceptUser-Agent 请求标头。否则,它会返回 503。这是非常糟糕的。它似乎也在进行用户代理嗅探。使用 curl 时出现 403:

    $ curl --head http://www.nativeseeds.org/
    HTTP/1.1 403 Forbidden
    Date: Wed, 26 Sep 2012 14:54:59 GMT
    Server: Apache
    P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
    Set-Cookie: f65129b0cd2c5e10c387f919ac90ad66=PjZxNjvNmn6IlVh4Ac-tH0; path=/
    Vary: Accept-Encoding
    Content-Type: text/html
    

    但如果我将用户代理设置为 Firefox,则可以正常工作:

    $ curl --user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" --head http://www.nativeseeds.org/
    HTTP/1.1 200 OK
    Date: Wed, 26 Sep 2012 14:55:57 GMT
    Server: Apache
    P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
    Expires: Mon, 1 Jan 2001 00:00:00 GMT
    Cache-Control: post-check=0, pre-check=0
    Pragma: no-cache
    Set-Cookie: f65129b0cd2c5e10c387f919ac90ad66=ykOpGnEE%2CQOMUaVJLnM7W0; path=/
    Last-Modified: Wed, 26 Sep 2012 14:56:27 GMT
    Vary: Accept-Encoding
    Content-Type: text/html; charset=utf-8
    

    使用requests 模块似乎可以工作:

    >>> import requests
    >>> r = requests.head('http://www.nativeseeds.org/')
    >>> import pprint; pprint.pprint(r.headers)
    {'cache-control': 'post-check=0, pre-check=0',
     'content-encoding': 'gzip',
     'content-length': '20',
     'content-type': 'text/html; charset=utf-8',
     'date': 'Wed, 26 Sep 2012 14:58:05 GMT',
     'expires': 'Mon, 1 Jan 2001 00:00:00 GMT',
     'last-modified': 'Wed, 26 Sep 2012 14:58:09 GMT',
     'p3p': 'CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"',
     'pragma': 'no-cache',
     'server': 'Apache',
     'set-cookie': 'f65129b0cd2c5e10c387f919ac90ad66=2NtRrDBra9jPsszChZXDm2; path=/',
     'vary': 'Accept-Encoding'}
    

    【讨论】:

      【解决方案2】:

      您看到的问题与 Python 无关。该网站本身似乎需要的不仅仅是 HEAD 请求。即使是简单的 telnet 会话也会导致错误:

      $ telnet www.nativeseeds.org 80
      Trying 208.113.230.85...
      Connected to www.nativeseeds.org (208.113.230.85).
      Escape character is '^]'.
      HEAD / HTTP/1.1
      Host: www.nativeseeds.org
      
      HTTP/1.1 503 Service Temporarily Unavailable
      Date: Wed, 26 Sep 2012 14:29:33 GMT
      Server: Apache
      Vary: Accept-Encoding
      Connection: close
      Content-Type: text/html; charset=iso-8859-1
      

      尝试添加更多标题; http 命令行客户端确实收到 200 响应:

      $ http -v head http://www.nativeseeds.org
      HEAD / HTTP/1.1
      Host: www.nativeseeds.org
      Content-Type: application/x-www-form-urlencoded; charset=utf-8
      Accept-Encoding: identity, deflate, compress, gzip
      Accept: */*
      User-Agent: HTTPie/0.2.2
      
      HTTP/1.1 200 OK
      Date: Wed, 26 Sep 2012 14:33:21 GMT
      Server: Apache
      P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
      Expires: Mon, 1 Jan 2001 00:00:00 GMT
      Cache-Control: post-check=0, pre-check=0
      Pragma: no-cache
      Set-Cookie: f65129b0cd2c5e10c387f919ac90ad66=34hOijDSzeskKYtULx9V83; path=/
      Last-Modified: Wed, 26 Sep 2012 14:33:23 GMT
      Vary: Accept-Encoding
      Content-Encoding: gzip
      Content-Length: 20
      Content-Type: text/html; charset=utf-8
      

      【讨论】:

      • Martijin 感谢回复 我需要添加哪些标题以及如何添加?
      • 如果您添加 AcceptUser-Agent 标头,您会得到 200 而不是 503。这个网站严重损坏
      【解决方案3】:

      Reading urllib2 docs,get_method 只返回 'GET' 或 'POST'。

      You may be interested in this.

      【讨论】:

      • bertobot 这不适用于上面的 URL,它仍然返回状态码 503
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多