1

我正在尝试使用以下代码检查 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 状态码。

任何帮助将不胜感激。

4

3 回答 3

4

经过一番调查,该网站要求同时设置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'}
于 2012-09-26T14:56:57.630 回答
3

您看到的问题与 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
于 2012-09-26T14:34:10.143 回答
0

阅读 urllib2 文档,get_method 只返回 'GET' 或 'POST'。

你可能对此感兴趣。

于 2012-09-26T14:41:15.560 回答