実際、urllib2はHTTPHEADリクエストを実行できるようです。
上記の@retoがリンクしている質問は、urllib2にHEADリクエストを実行させる方法を示しています。
これが私の見解です:
import urllib2
# Derive from Request class and override get_method to allow a HEAD request.
class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
myurl = 'http://bit.ly/doFeT'
request = HeadRequest(myurl)
try:
response = urllib2.urlopen(request)
response_headers = response.info()
# This will just display all the dictionary key-value pairs. Replace this
# line with something useful.
response_headers.dict
except urllib2.HTTPError, e:
# Prints the HTTP Status code of the response but only if there was a
# problem.
print ("Error code: %s" % e.code)
Wiresharkネットワークプロトコルアナライザーのようなものでこれをチェックすると、GETではなく実際にHEADリクエストを送信していることがわかります。
これは、Wiresharkによってキャプチャされた、上記のコードからのHTTP要求と応答です。
HEAD / doFeT HTTP / 1.1
Accept-Encoding:identity
Host:bit.ly
Connection:close
User-Agent:Python-urllib / 2.7
HTTP / 1.1 301移動
サーバー:nginx
日付:2012年2月19日日曜日13:20:56 GMT
コンテンツタイプ:text / html; charset = utf-8
キャッシュ制御:プライベート; max-age = 90
場所:
http
://www.kidsidebyside.org/?p = 445 MIME-Version:1.0
Content-Length:127
接続:close
Set-Cookie:_bit = 4f40f738-00153-02ed0-421cf10a; domain = .bit.ly; Expires = Fri Aug 17 13:20:56 2012; path = /; HttpOnly
ただし、他の質問のコメントの1つで述べたように、問題のURLにリダイレクトが含まれている場合、urllib2はHEADではなく宛先に対してGET要求を実行します。本当にHEADリクエストのみを行いたい場合、これは大きな欠点になる可能性があります。
上記のリクエストにはリダイレクトが含まれます。Wiresharkによってキャプチャされた宛先へのリクエストは次のとおりです。
GET / 2009/05 / come-and-draw-the-circle-of-unity-with-us / HTTP / 1.1
Accept-Encoding:identity
Host:www.kidsidebyside.org
Connection:close
User-Agent:Python-urllib / 2.7
urllib2を使用する代わりに、JoeGregorioのhttplib2ライブラリを使用することもできます。
import httplib2
url = "http://bit.ly/doFeT"
http_interface = httplib2.Http()
try:
response, content = http_interface.request(url, method="HEAD")
print ("Response status: %d - %s" % (response.status, response.reason))
# This will just display all the dictionary key-value pairs. Replace this
# line with something useful.
response.__dict__
except httplib2.ServerNotFoundError, e:
print (e.message)
これには、最初のHTTPリクエストと宛先URLへのリダイレクトされたリクエストの両方にHEADリクエストを使用するという利点があります。
これが最初のリクエストです:
HEAD / doFeT HTTP / 1.1
ホスト:bit.ly
accept-encoding:gzip、deflate
user-agent:Python-httplib2 / 0.7.2(gzip)
宛先への2番目のリクエストは次のとおりです。
HEAD / 2009/05 / come-and-draw-the-circle-of-unity-with-us / HTTP / 1.1
ホスト:www.kidsidebyside.org
accept-encoding:gzip、deflate
user-agent:Python-httplib2 / 0.7 .2(gzip)