Python httplib2 Handling Exceptions

后端 未结 2 905
名媛妹妹
名媛妹妹 2021-02-06 15:13

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\")         


        
相关标签:
2条回答
  • 2021-02-06 15:22
    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).

    0 讨论(0)
  • 2021-02-06 15:28
    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.

    0 讨论(0)
提交回复
热议问题