5

I have this very simple code to check if a site is up or down.

import httplib2
h = httplib2.Http()
response, content = h.request("http://www.folksdhhkjd.com")
if response.status == 200:
    print "Site is Up"
else:
    print "Site is down"

When I enter a valid URL then it properly prints Site is Up because the status is 200 as expected. But, when I enter an invalid URL, should it not print Site is down? Instead it prints an exception something like this

Traceback (most recent call last):
  File "C:\Documents and Settings\kripya\Desktop\1.py", line 3, in <module>
    response, content = h.request("http://www.folksdhhkjd.com")
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1436, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1188, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1129, in _conn_request
    raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
ServerNotFoundError: Unable to find the server at www.folksdhhkjd.com

How can I override this exception and print my custom defined "Site is down" message? Any guidance, please?

EDIT

Also one more question... what is the difference between using

h = httplib2.Http('.cache')   

and

h = httplib2.Http()   
Bhavani Kannan
  • 1,269
  • 10
  • 29
  • 46

2 Answers2

7
try:
    response, content = h.request("http://www.folksdhhkjd.com")
    if response.status==200:
        print "Site is Up"
except httplib2.ServerNotFoundError:
    print "Site is Down"

The issue with your code is that if the host doesn't respond, the request doesn't return ANY status code, and so the library throws an error (I think it's a peculiarity of the library itself, doing some sort of DNS resolution before trying to make the request).

jlmcdonald
  • 13,408
  • 2
  • 54
  • 64
4
h = httplib2.Http('.cache')   

Caches the stuff it retrieves in a directory called .cache so if you do the same request twice it might not have to actually get everything twice; a file starting with a dot is hidden in POSIX filesystems (like on Linux).

h = httplib2.Http()

Doesn't cache it's results, so you have to get everything requested every time.

agf
  • 171,228
  • 44
  • 289
  • 238
  • Oh, so... sounds like best practice is to use `h = httplib2.Http('.cache') ` then. Thanks a lot! – Bhavani Kannan Jul 20 '11 at 05:30
  • I Voted up both the answers anyway. Thanks! – Bhavani Kannan Jul 20 '11 at 06:02
  • I am trying to loop through the URLS using a file like this `sf = open('C:/Documents and Settings/user/Desktop/1.txt', 'r') for line in sf: active = 0 print "Current URL is: \t" + line try: response, content = h.request(line) if response.status==200: active=1 except httplib2.ServerNotFoundError: print "Site is Down" sf.close()` One of the URLs in the file is http://www.fastbookmarking.info. The site is up. I checked the Status code and its 200 – Bhavani Kannan Jul 20 '11 at 09:43
  • but for some reason.. it shows me "Site is down" Can anybody explain why? – Bhavani Kannan Jul 20 '11 at 09:44
  • 1
    It's a problem with the line in the file you're reading. Try `h.request(line.strip())`, and generally make sure the URL is EXACTLY right. – agf Jul 20 '11 at 09:50
  • hey, line.strip() did work! But can you please tell what is wrong with the URL when I use simply line? Because I printed both line and line.strip() both looks the same to me. Sorry! – Bhavani Kannan Jul 20 '11 at 09:54
  • One of them has whitespace at the end, most likely a line break in the form of a newline '\n', carriage return, '\r', or both. – agf Jul 20 '11 at 10:07
  • Oh, yes when I say print line it does print the content of the line with a new line character in between! I understand now. Thanks! – Bhavani Kannan Jul 20 '11 at 10:13