testing a website from python
Josef Meile
jmeile at hotmail.com
Wed Sep 21 17:51:27 EDT 2005
Hi,
> I just want to test that a given website is up or not from a python
> script. I thought of using wget as an os command. Any better ideas?
Here is how I did it:
import urllib2
import socket
def checkUrl(url, timeout=1):
"""Checks an url for a python version greater
than 2.3.3.
Note: For me isn't important the kind of
HTTP Error. If you want to know it,
then take a look at the existent
solutions like CMFLinkChecker and
see how they extract the code from
the error message.
"""
error={'code':1, 'msg':'Success'}
defTimeOut=socket.getdefaulttimeout()
socket.setdefaulttimeout(timeout)
try:
urllib2.urlopen(url)
except (urllib2.HTTPError, urllib2.URLError,
socket.error, socket.sslerror):
error['code']=-2
error['msg']="The link: \"${link}\" couldn't be validated"
except:
socket.setdefaulttimeout(defTimeOut)
raise
socket.setdefaulttimeout(defTimeOut)
return error
More information about the Python-list
mailing list