2

Possible Duplicate:
web2py url validator

would you help me on this code?

from urllib2 import Request, urlopen, URLError

url = raw_input('enter something')
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:
        print 'URL is good!'
Community
  • 1
  • 1
Hojat Taheri
  • 177
  • 1
  • 1
  • 11
  • 1
    please edit your post and format it for readability - what have you tried? what exactly is your question? – Inbar Rose Aug 16 '12 at 08:10
  • i want to ask user to enter an url then validate it.. – Hojat Taheri Aug 16 '12 at 08:12
  • `Traceback (most recent call last): File "C:/Users/r00t-7/PycharmProjects/untitled/sample.py", line 7, in response = urlopen(req) File "C:\Python25\lib\urllib2.py", line 121, in urlopen return _opener.open(url, data) File "C:\Python25\lib\urllib2.py", line 366, in open protocol = req.get_type() File "C:\Python25\lib\urllib2.py", line 241, in get_type raise ValueError, "unknown url type: %s" % self.__original ValueError: unknown url type: www.google.com Process finished with exit code 1` – Hojat Taheri Aug 16 '12 at 08:14
  • 1
    I think your `else` clause needs to be un-indented. Now it will only print "Url is good!" when there is an URLError. –  Aug 16 '12 at 08:25

4 Answers4

3

If you are trying to implement to code from web2py url validator, you will notice that you have added and indent to the else where none is needed. White space is important in python. The code that was given in my previous answer is correct, you have just copied it incorrectly. Your code should read like this (the same as my previous answer):

from urllib2 import Request, urlopen, URLError

url = raw_input('enter something')
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:
    print 'URL is good!'

The else clause is part of the try except not part of the exception test. Basically if an exception is not thrown, the url is valid. The following code gives you this result if you enter http://www.google.com

python test.py 
enter somethinghttp://www.google.com
URL is good!

If you enter http://www.google.com/bad you get:

python test.py 
enter somethinghttp://www.google.com/bad
The server couldn't fulfill the request.
Error code:  404
Community
  • 1
  • 1
BigHandsome
  • 4,843
  • 5
  • 23
  • 30
  • i wrote this in a neat way in python, now i have to use it in web2py, this is my code written with your help, thanks all, if you think that it can be better please comment it, i love u guys.. by the way, because i'm new to stackoverflow i can post my answear 4 hours later.. – Hojat Taheri Aug 16 '12 at 11:32
  • 1
    Glad to help. If there is a correct answer to your questions make sure to select it so the person that helped gets rewarded. – BigHandsome Aug 16 '12 at 11:39
  • sure but the problem is that my reputation is under 15 :))) – Hojat Taheri Aug 16 '12 at 11:52
  • 1
    Your rep does not need to be over 15 to accept an answer, only to upvote a question or an answer. To select and answer see: http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – BigHandsome Aug 16 '12 at 12:29
2

Try entering the full URL in your input:

entersomething http://www.google.com

You need to specify the type of request in order for it to understand handle it properly (in this case, http).

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
0

prefix URL with http://

example http://www.google.com

In [16]: response = urllib2.urlopen("http://www.google.com")

In [17]: response
Out[17]: <addinfourl at 28222408 whose fp = <socket._fileobject object at 0x01AE59B0>>

The urllib2 module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more.

avasal
  • 14,350
  • 4
  • 31
  • 47
  • well, i tried to check the protocol in this way, any better idea? `if url[0:7] == 'http://' or url[0:8] == 'https://' or url[0:6] =='ftp://': [do something with url..]` – Hojat Taheri Aug 16 '12 at 09:24
0

The stack you provided shows that you are getting a ValueError

"C:\Python25\lib\urllib2.py", line 241, in get_type raise ValueError, "unknown url type: %s" % self.__original ValueError: unknown url type: www.google.com

So you can add another except clause for ValueError to inform the user the url is not valid.

Or if you plan to correct the url, use url.lower().startswith('http://') or ...

Also note that urlopen can throw many other exceptions so you might also want to catch a generic Exception. You can find a more detailed discussion here

Community
  • 1
  • 1
Mihai Stan
  • 1,052
  • 6
  • 7