def _open_monkey(self, link): # requests does not support file:// -- so we must short-circuit manually if link.local: return open(link.local_path, 'rb') # noqa: T802 for attempt in range(self._max_retries + 1): try: return StreamFilelike(self._session.get( link.url, verify=self._verify, stream=True, headers={'User-Agent': self.USER_AGENT}, timeout=_REQUESTS_TIMEOUTS), link) except requests.exceptions.ReadTimeout: # Connect timeouts are handled by the HTTPAdapter, unfortunately read timeouts are not # so we'll retry them ourselves. logger.warning(f'Read timeout trying to fetch {link.url}, retrying. ' f'{self._max_retries - attempt} retries remain.') except requests.exceptions.RequestException as e: raise self.Error(e) raise self.Error( requests.packages.urllib3.exceptions.MaxRetryError( None, link, 'Exceeded max retries of %d' % self._max_retries))
def test_stream_filelike_without_md5(): with make_url(BLOB) as url: request = requests.get(url) filelike = StreamFilelike(request, Link.wrap(url)) assert filelike.read() == BLOB
def test_stream_filelike_with_incorrect_md5(): with make_url(BLOB, 'f' * 32) as url: request = requests.get(url) filelike = StreamFilelike(request, Link.wrap(url)) with pytest.raises(Context.Error): filelike.read()