1
0
Fork 0

Improve error handling.

If the response to the HTTP request isn't 200 (success), then don't save
the response, and don't call the callback.

Additionally, only retry in the case of HTTPException. This allows using
Ctrl-C to work correctly (and easily).
This commit is contained in:
Saikrishna Arcot 2016-12-27 20:46:59 -06:00
parent 9b38c0046f
commit 2c5429c589
No known key found for this signature in database
GPG key ID: 8216AAC71778097B

View file

@ -22,7 +22,7 @@
import urllib, os, hashlib
from urllib.parse import urlparse
from http.client import HTTPConnection, _CS_IDLE
from http.client import HTTPConnection, _CS_IDLE, HTTPException
from os import listdir
from os.path import isfile, join
@ -54,7 +54,7 @@ class HTTPGetter:
try:
self.doGet(httpGetCallback)
except:
except HTTPException:
# try to reconnect once
#print("reconnect")
self.httpConnection.close()
@ -117,6 +117,9 @@ class HTTPDownloadRequest(HTTPGetCallback):
self.mycallback = callback
def callback(self):
if self.result.status != 200:
return
with open(self.dst, 'wb') as f:
f.write(self.result.read())