Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get HTTP return code from python urllib's urlopen?

I have the following code:

f = urllib.urlopen(url)
html = f.read()

I would like to know the HTTP status code (HTTP 200, 404 etc) that comes from opening the url above.

Anybody knows how it can be done?

P.S. I use python 2.5.

Thanks!!!

like image 903
diemacht Avatar asked Feb 10 '13 09:02

diemacht


2 Answers

You can use the .getcode() method of the object returned by urlopen()

url = urllib.urlopen('http://www.stackoverflow.com/')
code = url.getcode()
like image 136
Forhad Ahmed Avatar answered Oct 12 '22 23:10

Forhad Ahmed


getcode() was only added in Python 2.6. As far as I know, there is no way to get the status code from the request itself in 2.5 but FancyURLopener provides a set of functions which get called on certain error codes - you could potentially use that to save a status code somewhere. I subclassed it to tell me when a 404 occurred

import urllib

class TellMeAbout404s(urllib.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())

info() provides the HTTP headers but not the status code.

like image 38
m.brindley Avatar answered Oct 12 '22 22:10

m.brindley