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:
parent
9b38c0046f
commit
2c5429c589
1 changed files with 5 additions and 2 deletions
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
import urllib, os, hashlib
|
import urllib, os, hashlib
|
||||||
from urllib.parse import urlparse
|
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 import listdir
|
||||||
from os.path import isfile, join
|
from os.path import isfile, join
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ class HTTPGetter:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.doGet(httpGetCallback)
|
self.doGet(httpGetCallback)
|
||||||
except:
|
except HTTPException:
|
||||||
# try to reconnect once
|
# try to reconnect once
|
||||||
#print("reconnect")
|
#print("reconnect")
|
||||||
self.httpConnection.close()
|
self.httpConnection.close()
|
||||||
|
@ -117,6 +117,9 @@ class HTTPDownloadRequest(HTTPGetCallback):
|
||||||
self.mycallback = callback
|
self.mycallback = callback
|
||||||
|
|
||||||
def callback(self):
|
def callback(self):
|
||||||
|
if self.result.status != 200:
|
||||||
|
return
|
||||||
|
|
||||||
with open(self.dst, 'wb') as f:
|
with open(self.dst, 'wb') as f:
|
||||||
f.write(self.result.read())
|
f.write(self.result.read())
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue