Python httplib2 异常未按预期抛出
Python httplib2 exception not thrown as expected
我写了一个脚本来 运行 python http 请求函数
代码如下:
header = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {'username':"admin", 'passwd':"passwd"}
data = urllib.urlencode(data)
http = httplib2.Http()
http.force_exception_to_status_code = False
try:
print "force_exception_to_status_code"+http.force_exception_to_status_code
response, content = http.request(self.url, 'POST', headers=header, body=data)
print response
except httplib2.RedirectMissingLocation:
print "Error"
控制台给了我:
force_exception_to_status_code=真
{'status': '302', 'transfer-encoding': 'chunked', 'server'.....)
但是没有抛出异常,即使我已经将 force_exception_to_status_code 设置为 false。
谁能告诉我这是什么问题?
查看 httplib2 的 github repository,您会发现在第 1367 行和第 1400 行之间,如果状态为 300,则会引发 RedirectMissingLocation。但是,对于 302,它不会引发您要捕获的异常。
我写了一个脚本来 运行 python http 请求函数 代码如下:
header = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {'username':"admin", 'passwd':"passwd"}
data = urllib.urlencode(data)
http = httplib2.Http()
http.force_exception_to_status_code = False
try:
print "force_exception_to_status_code"+http.force_exception_to_status_code
response, content = http.request(self.url, 'POST', headers=header, body=data)
print response
except httplib2.RedirectMissingLocation:
print "Error"
控制台给了我:
force_exception_to_status_code=真
{'status': '302', 'transfer-encoding': 'chunked', 'server'.....)
但是没有抛出异常,即使我已经将 force_exception_to_status_code 设置为 false。
谁能告诉我这是什么问题?
查看 httplib2 的 github repository,您会发现在第 1367 行和第 1400 行之间,如果状态为 300,则会引发 RedirectMissingLocation。但是,对于 302,它不会引发您要捕获的异常。