import urllib.request, urllib.parse, urllib.error


class TellMeAbout404s(urllib.request.FancyURLopener):

    def http_error_404(self, url, fp, errcode, errmsg, headers, data=None):
        print('==== Got a 404')
opener = TellMeAbout404s()
f = opener.open('http://www.google.com/sofbewfwl')
print(f.info())
How to get HTTP return code from python urllib's urlopen?