为什么会引发http.client.IncompleteRead异常?

时间:2019-04-04 15:30:44

标签: python-3.x urllib

我有以下Python 3代码序列,可从网络服务器下载zip存档并提取内容,但所有内容都被超时包裹了。

timeout_period = 60000
extraction_dir = /tmp/archive_contents
response = urllib.request.urlopen("http://webserver.com/archive.zip")
timer = threading.Timer(timeout_period, response.close)

timer.start()

try:
    with zipfile.ZipFile(io.BytesIO(response.read())) as zip_archive:
        zip_archive.extractall(extraction_dir)
except AttributeError:
    # Thrown by the object read() method when the fp is closed by the Timer thread
    pass

timer.cancel()

有时候,我会看到一个http.client.IncompleteRead异常。

我不认为该异常与超时相关,因为它发生在超时期限用尽之前,而且很容易重现超时期限并看到总是引发AttributeError异常。

为什么会这样,我该如何解决?

我可以捕获它并完成它,但是我想知道为什么这个异常到达了我的代码,因为我没有直接使用http.client模块。不应由urllib处理,最终抛出其他与http.client不相关的异常?

0 个答案:

没有答案