如何区分Python中的超时错误和其他“URLError”?

发布于 2024-10-26 22:20:26 字数 174 浏览 3 评论 0原文

如何区分Python中的超时错误和其他URLError

编辑

当我捕获URLError时,可能是名称解析暂时失败超时,或其他一些错误?我如何区分一个和另一个?

How to differentiate timeout error and other URLErrors in Python?

EDIT

When I catch a URLError, it can be Temporary failure in name resolution or timeout, or some other error? How can I tell one from another?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2024-11-02 22:20:26

我使用像下面的选项 2 这样的代码...但要获得全面的答案,请查看 Michael Foord 的 urllib2 页面

如果您使用下面的选项 1 或选项 2,您可以通过查看 e.code在 except 子句中添加任意多的智能和分支。 >e.reason

选项 1:

from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
try:
    response = urlopen(req)
except HTTPError, e:
    print 'The server couldn\'t fulfill the request.'
    print 'Error code: ', e.code
except URLError, e:
    print 'We failed to reach a server.'
    print 'Reason: ', e.reason
else:
    # everything is fine

选项 2:

from urllib import urlencode
from urllib2 import Request
# insert other code here...
    error = False
    error_code = ""
    try:
        if method.upper()=="GET":
            response = urlopen(req)
        elif method.upper()=="POST":
            response = urlopen(req,data)
    except IOError, e:
        if hasattr(e, 'reason'):
            #print 'We failed to reach a server.'
            #print 'Reason: ', e.reason
            error = True
            error_code = e.reason
        elif hasattr(e, 'code'):
            #print 'The server couldn\'t fulfill the request.'
            #print 'Error code: ', e.code
            error = True
            error_code = e.code
    else:
        # info is dictionary of server parameters, such as 'content-type', etc...
        info = response.info().dict
        page = response.read()

I use code like Option 2, below... but for a comprehensive answer, look at Michael Foord's urllib2 page

If you use either option 1 or option 2 below, you can add as much intelligence and branching as you like in the except clauses by looking at e.code or e.reason

Option 1:

from urllib2 import Request, urlopen, URLError, HTTPError
req = Request(someurl)
try:
    response = urlopen(req)
except HTTPError, e:
    print 'The server couldn\'t fulfill the request.'
    print 'Error code: ', e.code
except URLError, e:
    print 'We failed to reach a server.'
    print 'Reason: ', e.reason
else:
    # everything is fine

Option 2:

from urllib import urlencode
from urllib2 import Request
# insert other code here...
    error = False
    error_code = ""
    try:
        if method.upper()=="GET":
            response = urlopen(req)
        elif method.upper()=="POST":
            response = urlopen(req,data)
    except IOError, e:
        if hasattr(e, 'reason'):
            #print 'We failed to reach a server.'
            #print 'Reason: ', e.reason
            error = True
            error_code = e.reason
        elif hasattr(e, 'code'):
            #print 'The server couldn\'t fulfill the request.'
            #print 'Error code: ', e.code
            error = True
            error_code = e.code
    else:
        # info is dictionary of server parameters, such as 'content-type', etc...
        info = response.info().dict
        page = response.read()
橙幽之幻 2024-11-02 22:20:26

我使用以下代码来区分超时错误和其他 URLError

except URLError, e:
    if e.reason.message == 'timed out':
        # handle timed out exception
    else:
        # other URLError

I use the following code to differentiate timeout Error and other URLError

except URLError, e:
    if e.reason.message == 'timed out':
        # handle timed out exception
    else:
        # other URLError
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文