Timeouts with urllib2

Skip Montanaro skip at pobox.com
Mon Mar 15 20:46:12 CET 2004


    Fazer> I was wondering what's the best way of setting timeouts when
    Fazer> working with urllib2.

Using 2.3+:

    >>> import socket
    >>> socket.setdefaulttimeout(3)
    >>> import urllib2
    >>> urllib2.urlopen("http://www.musi-cal.com/search?performers=Greg+Brown")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "/Users/skip/local/lib/python2.4/urllib2.py", line 130, in urlopen
        return _opener.open(url, data)
      File "/Users/skip/local/lib/python2.4/urllib2.py", line 338, in open
        response = self._open(req, data)
      File "/Users/skip/local/lib/python2.4/urllib2.py", line 356, in _open
        '_open', req)
      File "/Users/skip/local/lib/python2.4/urllib2.py", line 317, in _call_chain
        result = func(*args)
      File "/Users/skip/local/lib/python2.4/urllib2.py", line 988, in http_open
        return self.do_open(httplib.HTTPConnection, req)
      File "/Users/skip/local/lib/python2.4/urllib2.py", line 971, in do_open
        raise URLError(err)
    urllib2.URLError: <urlopen error timed out>

Skip




More information about the Python-list mailing list