def _safe_read(opener, req, body=None, max_retries=3, retry_delay_delta=2.0):
 last_exc = None
 for retry_num in range(max_retries):
  if retry_num:
   _log.info(('retrying after delay (#%i)' % retry_num))
   time.sleep((retry_num * retry_delay_delta))
  try:
   if body:
    f = opener.open(req, body)
   else:
    f = opener.open(req)
   return f.read()
  except compat.HTTPError as exc:
   if (exc.code in (400, 404, 411)):
    raise ResponseError(cause=exc)
   elif (exc.code in (503, 502, 500)):
    _log.info(('HTTP error %i' % exc.code))
   elif (exc.code in (401,)):
    raise AuthenticationError(cause=exc)
   else:
    _log.info(('unknown HTTP error %i' % exc.code))
   last_exc = exc
  except compat.BadStatusLine as exc:
   _log.info('bad status line')
   last_exc = exc
  except compat.HTTPException as exc:
   _log.info(('miscellaneous HTTP exception: %s' % str(exc)))
   last_exc = exc
  except compat.URLError as exc:
   if isinstance(exc.reason, socket.error):
    code = exc.reason.errno
    if (code == 104):
     continue
   raise NetworkError(cause=exc)
  except socket.timeout as exc:
   _log.info('socket timeout')
   last_exc = exc
  except socket.error as exc:
   if (exc.errno == 104):
    continue
   raise NetworkError(cause=exc)
  except IOError as exc:
   raise NetworkError(cause=exc)
 raise NetworkError(('retried %i times' % max_retries), last_exc)

def _safe_read ( opener , req , body = None , max_retries = 3 , retry_delay_delta = 2.0 ) : last_exc = None for retry_num in range ( max_retries ) : if retry_num : _log . info ( ( 'retrying after delay (#%i)' % retry_num ) ) time . sleep ( ( retry_num * retry_delay_delta ) ) try : if body : f = opener . open ( req , body ) else : f = opener . open ( req ) return f . read ( ) except compat . HTTPError as exc : if ( exc . code in ( 400 , 404 , 411 ) ) : raise ResponseError ( cause = exc ) elif ( exc . code in ( 503 , 502 , 500 ) ) : _log . info ( ( 'HTTP error %i' % exc . code ) ) elif ( exc . code in ( 401 , ) ) : raise AuthenticationError ( cause = exc ) else : _log . info ( ( 'unknown HTTP error %i' % exc . code ) ) last_exc = exc except compat . BadStatusLine as exc : _log . info ( 'bad status line' ) last_exc = exc except compat . HTTPException as exc : _log . info ( ( 'miscellaneous HTTP exception: %s' % str ( exc ) ) ) last_exc = exc except compat . URLError as exc : if isinstance ( exc . reason , socket . error ) : code = exc . reason . errno if ( code == 104 ) : continue raise NetworkError ( cause = exc ) except socket . timeout as exc : _log . info ( 'socket timeout' ) last_exc = exc except socket . error as exc : if ( exc . errno == 104 ) : continue raise NetworkError ( cause = exc ) except IOError as exc : raise NetworkError ( cause = exc ) raise NetworkError ( ( 'retried %i times' % max_retries ) , last_exc )
reads large blocks in a safe way .