Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用urllib捕获超时错误_Python_Python 3.x - Fatal编程技术网

Python 如何使用urllib捕获超时错误

Python 如何使用urllib捕获超时错误,python,python-3.x,Python,Python 3.x,我正在努力尝试,除了python 3 我可以捕获HTTPErrorBad请求和URLErrorno主机/路由名称 但是,我还不能捕获超时错误 while True: try: f = urllib.request.urlretrieve(url,csvname) except urllib.error.HTTPError as e: # print(str(chl) + " Error:" + str(e))

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

我可以捕获HTTPErrorBad请求和URLErrorno主机/路由名称

但是,我还不能捕获超时错误

 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

在使用套接字之前,需要导入它:

from urllib import socket
或者指定您正在谈论的套接字来自urllib,即:

except urllib.socket.timeout as e:

在使用套接字之前,需要导入它:

from urllib import socket
或者指定您正在谈论的套接字来自urllib,即:

except urllib.socket.timeout as e:
在Python3.x上,TimeoutError是一个内置的异常类。你可以用手抓住它

except TimeoutError:
    ...
在Python2.x上,有两个选项:

捕获urllib.socket.timeout异常

捕获异常

在Python3.x上,TimeoutError是一个内置的异常类。你可以用手抓住它

except TimeoutError:
    ...
在Python2.x上,有两个选项:

捕获urllib.socket.timeout异常

捕获异常


ImportError:无法导入名称“socket”,我尝试了上一个,但遇到了此错误。您是否尝试从此处运行代码:可能他们在python 3中更改了它,但注释表明您不需要套接字,URLerror捕获了这两个。ImportError:无法导入名称“socket”,我尝试了上面的一个,但遇到了此错误。您是否尝试从此处运行代码:可能他们在python 3中对其进行了更改,但注释表明您不需要套接字,URLerror捕获了这两个名称。