web2py url validator

旧巷老猫 提交于 2019-12-31 04:13:14

问题


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.) but i don't get what's wrong..!!

import urllib

def index():
    return dict()

def random_maker():
    url = request.vars.url
    try:
        urllib.urlopen(url)
        return dict(rand_url = ''.join(random.choice(string.ascii_uppercase +
                    string.digits + string.ascii_lowercase) for x in range(6)),
                    input_url=url)
    except IOError:
        return index()

回答1:


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.



来源:https://stackoverflow.com/questions/11971369/web2py-url-validator

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