diff --git a/plugins/Web/config.py b/plugins/Web/config.py index 2ce3e3bfc..f22e0ad15 100644 --- a/plugins/Web/config.py +++ b/plugins/Web/config.py @@ -58,4 +58,9 @@ conf.registerGlobalValue(Web.fetch, 'maximum', registry.NonNegativeInteger(0, """Determines the maximum number of bytes the bot will download via the 'fetch' command in this plugin.""")) +conf.registerGlobalValue(Web.fetch, 'timeout', + registry.NonNegativeInteger(5, """Determines the maximum number of + seconds the bot will wait for the site to respond, when using the 'fetch' + command in this plugin. If 0, will use socket.defaulttimeout""")) + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/plugins/Web/plugin.py b/plugins/Web/plugin.py index 78ba0f0d3..00cd104e7 100644 --- a/plugins/Web/plugin.py +++ b/plugins/Web/plugin.py @@ -236,7 +236,10 @@ class Web(callbacks.PluginRegexp): irc.error('This command is disabled ' '(supybot.plugins.Web.fetch.maximum is set to 0).', Raise=True) - fd = utils.web.getUrlFd(url) + timeout = self.registryValue('fetch.timeout') + if timeout == 0: + timeout = None + fd = utils.web.getUrlFd(url, timeout=timeout) irc.reply(fd.read(max)) fetch = wrap(fetch, ['url']) diff --git a/src/utils/web.py b/src/utils/web.py index cbd0ecc73..f460bb1f6 100644 --- a/src/utils/web.py +++ b/src/utils/web.py @@ -96,7 +96,7 @@ defaultHeaders = { # application-specific function. Feel free to use a callable here. proxy = None -def getUrlFd(url, headers=None, data=None): +def getUrlFd(url, headers=None, data=None, timeout=None): """getUrlFd(url, headers=None, data=None) Opens the given url and returns a file object. Headers and data are @@ -114,7 +114,7 @@ def getUrlFd(url, headers=None, data=None): httpProxy = force(proxy) if httpProxy: request.set_proxy(httpProxy, 'http') - fd = urllib2.urlopen(request) + fd = urllib2.urlopen(request, timeout=timeout) return fd except socket.timeout, e: raise Error, TIMED_OUT