For my project, I have to check the status of a website (on a shared hosting).
I use Python requests library.
def getStatusCode(url):
try:
return requests.head(url,timeout=0.3).status_code
except:
return -1
This code works great under MacOS 10.10 with Python3.4 with an url like http://www.google.com. If I unplug my ISP cable, I immediately got an exception.
Under Ubuntu Server 14.04 with Python3.4, if I unplug my ISP cable, I never get a timeout error. Same problem on Raspbian.
After some tests, if I replace the url with an IP http://216.58.212.100, Ubuntu Server raise me an exception, but as I'm on a shared web hosting so I can't use an IP.
After some research I found there is a difference between timeout in requests library and DNS lookup that not performed by it but by the OS.
So my question is what is the most beautiful way to solve this ? Do I need to add extra timeout exception in Python like : Timeout on a function call
Thank you