【问题标题】:How to catch timeout error with urllib如何使用 urllib 捕获超时错误
【发布时间】:2018-01-08 15:05:58
【问题描述】:

我正在努力尝试,除了 python 3

我可以捕捉到 HTTPError(Bad Request) 和 URLError(no host/route name found)

但是我还没有捕捉到超时错误。

 while True:
        try:
            f = urllib.request.urlretrieve(url,csvname)
        except urllib.error.HTTPError as e: #
            print(str(chl) + " Error:" + str(e))


            continue
        except urllib.error.URLError as e: 
            continue
        except socket.timeout as e:
            #I can't catch time out error here
            continue

它返回这个。

如何防止脚本因超时错误而停止?

Traceback (most recent call last):
  File "wisdom2.py", line 84, in <module>
    f = urllib.request.urlretrieve(url,csvname)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 186, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 464, in open
    response = self._open(req, data)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 482, in _open
    '_open', req)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 442, in _call_chain
    result = func(*args)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 1211, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 1186, in do_open
    r = h.getresponse()
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/http/client.py", line 1227, in getresponse
    response.begin()
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/http/client.py", line 386, in begin
    version, status, reason = self._read_status()
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/http/client.py", line 348, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/socket.py", line 378, in readinto
    return self._sock.recv_into(b)
TimeoutError: [Errno 60] Operation timed out

     except socket.timeout as e:
NameError: name 'socket' is not defined

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    在 python 3.x 上,TimeoutError 是一个内置的异常类。你可以用

    来捕捉它
    except TimeoutError:
        ...
    

    在 python 2.x 上,您有两种选择:

    1. 捕捉urllib.socket.timeout异常

    2. 捕捉requests.Timeout异常

    【讨论】:

      【解决方案2】:

      使用前需要先导入socket:

      from urllib import socket
      

      或者指定你说的socket来自urllib,即:

      except urllib.socket.timeout as e:
      

      【讨论】:

      • ImportError: cannot import name 'socket' 我试过上面那个,碰到这个错误。
      • 你想从这里运行代码吗:stackoverflow.com/questions/2712524/…
      • 也许他们已经在 python 3 中更改了它,但是 cmets 建议您不需要套接字,URLerror 会同时捕获两者。
      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 2021-03-08
      • 2023-02-13
      相关资源
      最近更新 更多