2012-09-26 2 views
1

내가 같이 사용할 때 다음 코드HEAD 요청은 원하는대로 작동하지

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

를 사용하여 파이썬에서 모든 URL의 상태 코드를 확인하려고 :

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

을 다음과 같은 예외가 발생합니다 : 그러나

HTTPError: HTTP Error 503: Service Temporarily Unavailable 

을 나는 파이어 폭스/위해 RESTClient에서 위의 URL "http://www.nativeseeds.org/"을 열 때, 나는 t는 200 상태 코드를 반환합니다.

도움이 될 것입니다.

답변

3

당신이 보는 문제는 파이썬과 아무 관련이 없습니다. 웹 사이트 자체는 단순한 HEAD 요청 이상의 것을 필요로하는 것 같습니다. 간단한 텔넷 세션조차도 다음과 같은 오류가 발생합니다.

$ 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 
+0

을 Martijin이 회신을 보내 주셔서 감사합니다 어떤 헤더를 추가해야합니까? – Ibrahim

+1

"Accept"와 "User-Agent"헤더를 추가하면 503 대신 200이됩니다.이 웹 사이트는 끔찍하게 깨졌습니다. – jterrace

4

는 웹 사이트 모두 AcceptUser-Agent 요청 헤더가 설정되어가 필요합니다. 그렇지 않으면 503을 반환합니다. 이것은 심각하게 손상되었습니다. 또한 사용자 에이전트 스니핑을하고있는 것처럼 보입니다. 나는 파이어 폭스 사용자 에이전트를 설정하면

$ 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 

하지만 잘 작동 : 컬을 사용할 때 403를 얻을

requests 모듈을 사용하여 작업 표시
$ 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 

:

>>> 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'} 
관련 문제