From 02800a8fa14dffa2cf122ebc87842d67134f521b Mon Sep 17 00:00:00 2001 From: Arun Raghavan Date: Mon, 9 Mar 2009 00:29:34 +0530 Subject: Add a timeout for http/ftp connections urllib2 doesn't support specifying a timeout, so we set a global timeout using the socket class. --- check.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/check.py b/check.py index fba550b..0f6aebb 100644 --- a/check.py +++ b/check.py @@ -1,6 +1,7 @@ #!/usr/bin/python import logging +import socket import urllib2 import time import rfc822 @@ -12,13 +13,17 @@ import urlparse class GardCheck: # Base class which provides some helper functions - def __init__(self, url, log=True): + def __init__(self, url, log=True, timeout=30): self.url = url if log: self.logger = logging.getLogger('') else: self.logger = None + # By default urllib2 has no timeout, so we need to set a sane + # global maximum + socket.setdefaulttimeout(timeout) + def log_info(self, msg): if self.logger is not None: self.logger.info(msg) -- cgit v1.2.3-65-gdbad