how to check if URL cannot be opened

Josef Meile jmeile at hotmail.com
Wed Oct 27 15:09:03 EDT 2004


> This is the code I use with python greater than 2.3.3:
> 
> import urllib2
> import socket
> 
> def checkUrl(url, timeout=5, SSL=0):
>   """Checks an url for a python version greater
>      than 2.3.3.
>   """
> 
>   defTimeOut=socket.getdefaulttimeout()
>   socket.setdefaulttimeout(timeout)
>   found=1
>   try:
>     urllib2.urlopen(url)
>   except (urllib2.HTTPError, urllib2.URLError,
>           socket.error, socket.sslerror):
>     found=0
>   socket.setdefaulttimeout(defTimeOut)
>   return found
Ops, sorry, the SSL parameter isn't being used there, so you can delete it.



More information about the Python-list mailing list