如何从python urllib';什么时候开门?

如何从python urllib';什么时候开门?,python,urllib,python-2.5,Python,Urllib,Python 2.5,我有以下代码: f = urllib.urlopen(url) html = f.read() 我想知道通过打开上面的url得到的HTTP状态码(HTTP 200、404等) 有人知道怎么做吗 附言。 我使用Python2.5 谢谢 您可以使用urlopen() getcode()仅在Python 2.6中添加。据我所知,在2.5中无法从请求本身获取状态代码,但FancyURLopener提供了一组函数,可以在某些错误代码上调用这些函数-您可以使用这些函数将状态代码保存到某个位置。我将其子类化

我有以下代码:

f = urllib.urlopen(url)
html = f.read()
我想知道通过打开上面的url得到的HTTP状态码(HTTP 200、404等)

有人知道怎么做吗

附言。 我使用Python2.5


谢谢

您可以使用
urlopen()

getcode()
仅在Python 2.6中添加。据我所知,在2.5中无法从请求本身获取状态代码,但FancyURLopener提供了一组函数,可以在某些错误代码上调用这些函数-您可以使用这些函数将状态代码保存到某个位置。我将其子类化,以告诉我何时发生404

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()?你是说HTTP状态吗?
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())