简体   繁体   English

如何从python urllib的urlopen获取HTTP返回码?

[英]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. 我想知道打开上面的url所带来的HTTP状态代码(HTTP 200,404等)。

Anybody knows how it can be done? 谁知道怎么做?

PS I use python 2.5. PS我使用python 2.5。

Thanks!!! 谢谢!!!

You can use the .getcode() method of the object returned by urlopen() 你可以使用urlopen()返回的对象的.getcode()方法

url = urllib.urlopen('http://www.stackoverflow.com/')
code = url.getcode()

getcode() was only added in Python 2.6. getcode()仅在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. 据我所知,没有办法从2.5中的请求本身获取状态代码,但FancyURLopener提供了一组函数,可以调用某些错误代码 - 您可以使用它来保存状态代码。 I subclassed it to tell me when a 404 occurred 我将其子类化为告诉我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() provides the HTTP headers but not the status code. info()提供HTTP标头,但不提供状态代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM