Check URL --> Simply?

Peter Hansen peter at
Wed Aug 15 14:31:14 CEST 2001

Alex wrote:
> import urllib
> def checkURL(url):
>     try: urllib.urlopen(url)
>     except IOError: return 0
>     return 1
> if __name__ == '__main__':
>     assert checkURL('')
>     assert not checkURL('http://foo')

What does this do when a page doesn't really exist
but the server catches the 404 error and ends up serving
up a "this page doesn't exist page"?  I think that
might make almost any URL on modern servers look like
it actually exists, which may not be quite what the
OP really wanted.  For example:

>>> checkURL('')

(I realize the definition of "does URL exist" is
really the problem here, not the code.)

Peter Hansen, P.Eng.
peter at

More information about the Python-list mailing list