Search code examples
pythonhttplib2

Python httplib2 exception not thrown as expected


I wrote a script to run python http request functions The code is as following:

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"

The console gave me:

force_exception_to_status_code=True

{'status': '302', 'transfer-encoding': 'chunked', 'server'.....)

But the exception was not thrown, even if I have already set the force_exception_to_status_code to false.

Can anyone tell me what is the problem?


Solution

  • Look at httplib2's github repository and you will find between lines 1367 and 1400 that if the status is a 300 then it will raise RedirectMissingLocation. However for a 302 it will not raise the exception that you are looking to catch.