Python Mechanize.Browser.Open(url) Status Code

James picture James · Aug 4, 2012 · Viewed 7k times · Source

I recently moved from perl to python and am updating some of my scripts which used perls WWW::Mechanize to use python mechanize module.

All good so far but I cant get find the HTTP Status code anywhere? (200, 301, etc)

I've googled and found this but isnt their a way to just view the code?

Getting and trapping HTTP response using Mechanize in Python

Thanks.

Answer

enderskill picture enderskill · Aug 4, 2012

Use response.code

from mechanize import Browser
browser = Browser()
response = browser.open('http://www.google.com')
print response.code