Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). Is there a way to get the original status code?

Thanks

like image 938
user319723 Avatar asked Apr 20 '10 06:04

user319723


1 Answers

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
like image 152
Duncan Avatar answered Nov 13 '22 00:11

Duncan