1

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()
5
  • 1
    Is this you're complete code? I don't see any way to enter a URL (i.e., a form). Also, you need to import random and string. It would help if you could describe what you expect, what you're getting, and show any other relevant code (e.g., a view). Commented Aug 15, 2012 at 14:55
  • Wouldn't it be better to just check the url with a regex, take a look at this: stackoverflow.com/a/11967815/1248554 Commented Aug 15, 2012 at 15:31
  • No need for the regex, as web2py already includes an IS_URL validator. I assume, though, the OP wants to confirm that the URL points to a live site, not simply that it is a well-formed URL. Commented Aug 15, 2012 at 17:23
  • 1
    thanks 4 all, look when i use urllib.urlopen it opens a connection to website, right like httplib.HTTPConnect, but the problem is what happens when a user enters something like jfvbhsjdfvbs.com Commented Aug 15, 2012 at 17:31
  • the question is what shold i do with non responsive webpages..httplib..urllib.. or some th. else.. Commented Aug 15, 2012 at 17:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

def md5(): url = request.vars.url conn = httplib.HTTPConnection(url) try: conn.request("HEAD", "/") return dict(rand_url = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(6)), input_url=url) except StandardError: print "invalid URL" . . . . gaierror: (11004, 'getaddrinfo failed')
thanks, but the problem is web2py is full compatible with python 2.5, and i heard that python 2.5 only has urllib, is it possible to use urllib2 in python 2.5?
Yes, urllib2 should be in python 2.5. Here is the api reference for python 2.5 from the python doc site.
def random(): url = request.vars.url req = Request(url) 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: return dict(rand_url = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(6)), input_url=url)
i'm realy sorry to take your time, i'm disapointed.. i just wanted to check that the page that i wanted to shorten it's url is available and valid or not..sometimes writing codes decrease our speed and creative mind.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.