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()   

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

wow the basic exception handling is what I should learn now then... Thanks a lot!
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.

8 Comments

Oh, so... sounds like best practice is to use h = httplib2.Http('.cache') then. Thanks a lot!
I Voted up both the answers anyway. Thanks!
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 fastbookmarking.info. The site is up. I checked the Status code and its 200
but for some reason.. it shows me "Site is down" Can anybody explain why?
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.