Take the 2-minute tour ×

For a url entered by the user :

check that is any webserver hosting the domain name or not ?

For example :


Now, the above url does-not exists, so there is no point of check for any HTTP error codes since, no server is there that will reply back.

So, how to detect such url ?

isServerUp() ?
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

This should open the url for you; obviously change how you want to say that the url has failed to open. The docs are: http://docs.python.org/library/urllib2.html

from urllib2 import *

req = Request(url)

# Try to open the url
    reponse = urlopen(req)
except HTTPError, e:
    url = None
except URLError, e:
    url = None

EDIT: Indentation fixed

share|improve this answer
Please fix the indentation, I don't think that will run. –  dancek Aug 23 '11 at 10:38
@dancek thank you, that's what I get for copying and pasting. –  Ben Aug 23 '11 at 10:43
+1, but please note that import * is usually a bad practice cluttering your namespace and creating less maintainable code. either explicitly import what you need, or import the module. –  Ofri Raviv Aug 23 '11 at 10:51
its giving error : ValueError: unknown url type: thisisaurlthatdoesnotexists.com –  Yugal Jindle Aug 23 '11 at 10:52
@Yugal the url must be a string and must contain the http part: ("http://thisisaurlthatdoesnotexists.com") –  RxS Aug 23 '11 at 10:57
show 2 more comments

Your Answer


