Tanky Woo
Tanky Woo

Reputation: 5106

how can I detect whether the http and https service is ok in python?

I want to detect wheter the http or https service is ok, in python.

Now I have known is to use httplib module.

Use the httplib.HTTPConnection to get the status, and check whether it is 'OK'(code is 200), and the same to https by using HTTPSConnection

but I don't know whether this way is right?or there is another more good way?

Upvotes: 2

Views: 1557

Answers (1)

Emmanuel
Emmanuel

Reputation: 14209

I have a script that does this kind of check, I use urllib2 for that, whatever the protocol (http or https):

result = False
error = None
try:
    # Open URL
    urllib2.urlopen(url, timeout=TIMEOUT)
    result = True
except urllib2.URLError as exc:
    error = 'URL Error: {0}'.format(str(exc))
except urllib2.HTTPError as exc:
    error = 'HTTP Error: {0}'.format(str(exc))
except Exception as exc:
    error = 'Unknow error: {0}'.format(str(exc))

Upvotes: 3

Related Questions