|
46 | 46 | from six.moves import urllib
|
47 | 47 | from requests import Response
|
48 | 48 | from tqdm import tqdm
|
49 |
| -from requests.exceptions import HTTPError |
| 49 | +from requests.exceptions import HTTPError, ConnectionError |
| 50 | +from urllib3.exceptions import ProtocolError |
| 51 | +try: |
| 52 | + from http.client import RemoteDisconnected |
| 53 | +except ImportError: |
| 54 | + from httplib import BadStatusLine as RemoteDisconnected |
50 | 55 |
|
51 | 56 | from internetarchive.utils import IdentifierListAsItems, get_md5, chunk_generator, \
|
52 | 57 | IterableToFileAdapter, iter_directory, recursive_file_count, norm_filepath
|
@@ -1104,6 +1109,21 @@ def _build_request():
|
1104 | 1109 | print(' error uploading {0}: {1}'.format(key, msg), file=sys.stderr)
|
1105 | 1110 | # Raise HTTPError with error message.
|
1106 | 1111 | raise type(exc)(error_msg, response=exc.response, request=exc.request)
|
| 1112 | + except ConnectionError as exc: # from requests |
| 1113 | + exc = exc.args[0] |
| 1114 | + if isinstance(exc, ProtocolError): # from urllib3 |
| 1115 | + exc = exc.args[1] |
| 1116 | + if isinstance(exc, RemoteDisconnected): # from http.client |
| 1117 | + msg = ("The server closed the connection after the file was uploaded. " |
| 1118 | + "The upload might have succeeded anyway.") |
| 1119 | + error_msg = (' error uploading {0} to {1}, ' |
| 1120 | + '{2}'.format(key, self.identifier, msg)) |
| 1121 | + log.error(error_msg) |
| 1122 | + return Response() |
| 1123 | + else: |
| 1124 | + raise |
| 1125 | + else: |
| 1126 | + raise |
1107 | 1127 | finally:
|
1108 | 1128 | body.close()
|
1109 | 1129 |
|
|
0 commit comments