web2py url validator

泄露秘密 提交于 2019-12-02 03:24:10
BigHandsome

Couldn't you check the http response code using httplib. If it was 200 then the page is valid, if it is anything else (like 404) or an error then it is invalid.

See this question: What’s the best way to get an HTTP response code from a URL?

Update:

Based on your comment it looks like your issue is how you are handling the error. You are only handling IOError issues. In your case you can either handle all errors singularly by switching to:

except:
    return index()

You could also build your own exception handler by overriding http_default_error. See How to catch 404 error in urllib.urlretrieve for more information.

Or you can switch to urllib2 which has specific errors, You can then handle the specific errors that urllib2 throws like this:

from urllib2 import Request, urlopen, URLError
req = Request('http://jfvbhsjdfvbs.com')
try:
    response = urlopen(req)
except URLError, e:
    if hasattr(e, 'reason'):
        print 'We failed to reach a server.'
        print 'Reason: ', e.reason
    elif hasattr(e, 'code'):
        print 'The server couldn\'t fulfill the request.'
        print 'Error code: ', e.code
else:
    print 'URL is good!'

The above code with that will return:

We failed to reach a server.
Reason:  [Errno 61] Connection refused

The specifics of each exception class is contained in the urllib.error api documentation.

I am not exactly sure how to slot this into your code, because I am not sure exactly what you are trying to do, but IOError is not going to handle the exceptions thrown by urllib.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!