Python urllib2でレスポンス201はエラー扱いになる
urllib2 でエラーCommentsAdd Star - Djangoへの片思い日記
を読んで試してみました。確かにエラーになりますね。
import urllib2
class myHTTPErrorProcessor(urllib2.HTTPErrorProcessor):
def http_response(self, request, response):
code, msg, hdrs = response.code, response.msg, response.info()
if code >= 300:
response = self.parent.error(
'http', request, response, code, msg, hdrs)
return response
def urlopen(url):
try:
print urllib2.urlopen(url).read()
except urllib2.HTTPError, e:
print e
# default behavior
urlopen('http://hoge/201/')
# override HTTPErrorProcessor
opener = urllib2.build_opener(myHTTPErrorProcessor)
urllib2.install_opener(opener)
urlopen('http://hoge/201/')
昨日のPython Unconferenceの帰りに柴田さんに聞いたら、そういう仕様だそうです。。










