web2py url validator

前端 未结 1 1947
广开言路
广开言路 2021-01-24 21:49

In a shorten-er built by web2by i want to validate url\'s first, if it\'s not valid goes back to the first page with an error message. this is my code in controller (mvc arch.)

相关标签:
1条回答
  • 2021-01-24 22:30

    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.

    0 讨论(0)
提交回复
热议问题