请求达到超时处理

时间:2020-06-12 21:17:56

标签: python python-3.x python-2.7 python-requests urllib2

我有以下代码,它假定要下载文件。

print('getting:'+url)
resp = requests.get(url, headers=headers,cookies=CUSTOM_COOKIE, params=param,stream=True,timeout=20)

但在下载时会不断出现以下错误

Traceback (most recent call last):

  File "C:\Users\kkk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\response.py", line 437, in _error_catcher
    yield
  File "C:\Users\kkk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\response.py", line 519, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 454, in read
    n = self.readinto(b)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\lib\http\client.py", line 498, in readinto
    n = self.fp.readinto(b)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\lib\socket.py", line 669, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\kkk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests\models.py", line 751, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\kkk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\response.py", line 576, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "C:\Users\kkk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\response.py", line 541, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1008.0_x64__qbz5n2kfra8p0\lib\contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\kkk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\urllib3\response.py", line 442, in _error_catcher
    raise ReadTimeoutError(self._pool, None, "Read timed out.")
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='audiopay.cos.xmcdn.com', port=80): Read timed out.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "downloadximalaya.py", line 434, in <module>
    main()
  File "downloadximalaya.py", line 432, in main
    download()
  File "downloadximalaya.py", line 407, in download
    download_file(track["title"] + ".m4a", file_download_url,title)
  File "downloadximalaya.py", line 341, in download_file
    for data in resp_data.iter_content(chunk_size=chunk_size):
  File "C:\Users\kkk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\requests\models.py", line 758, in generate
    raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='audiopay.cos.xmcdn.com', port=80): Read timed out.

我想捕获此错误,我什至尝试了以下操作,但仍未捕获到异常

try:
    resp = requests.get(url, headers=headers,cookies=CUSTOM_COOKIE, params=param,stream=True,timeout=20)
except:
    print('exceptions here')

有人知道如何在这里捕获此异常吗? 谢谢

0 个答案:

没有答案
相关问题