Skip to content Skip to sidebar Skip to footer

Getting And Trapping Http Response Using Mechanize In Python

I am trying to get the response codes from Mechanize in python. While I am able to get a 200 status code anything else isn't returned (404 throws and exception and 30x is ignored).

Solution 1:

Errors will throw an exception, so just use try:...except:... to handle them.

Your Mechanize browser object has a method set_handle_redirect() that you can use to turn 30x redirection on or off. Turn it off and you get an error for redirects that you handle just like you handle any other error:

>>>from mechanize import Browser>>>browser = Browser()>>>resp = browser.open('http://www.oxfam.com') # this generates a redirect>>>resp.geturl()
'http://www.oxfam.org/'
>>>browser.set_handle_redirect(False)>>>resp = browser.open('http://www.oxfam.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 209, in open
  File "build\bdist.win32\egg\mechanize\_mechanize.py", line 261, in _mech_open
mechanize._response.httperror_seek_wrapper: HTTP Error 301: Moved Permanently
>>>>>>from urllib2 import HTTPError>>>try:...   resp = browser.open('http://www.oxfam.com')...except HTTPError, e:...print"Got error code", e.code...
Got error code 301

Solution 2:

In twill, do get_browser().get_code()

twill is an outstanding automation and test layer built on top of mechanize, to make it easier to use. It is seriously handy.

Post a Comment for "Getting And Trapping Http Response Using Mechanize In Python"