http.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:oscars2016 作者: 0x0ece 项目源码 文件源码
def _retry_request(http, num_retries, req_type, sleep, rand, uri, method, *args,
                   **kwargs):
  """Retries an HTTP request multiple times while handling errors.

  If after all retries the request still fails, last error is either returned as
  return value (for HTTP 5xx errors) or thrown (for ssl.SSLError).

  Args:
    http: Http object to be used to execute request.
    num_retries: Maximum number of retries.
    req_type: Type of the request (used for logging retries).
    sleep, rand: Functions to sleep for random time between retries.
    uri: URI to be requested.
    method: HTTP method to be used.
    args, kwargs: Additional arguments passed to http.request.

  Returns:
    resp, content - Response from the http request (may be HTTP 5xx).
  """
  resp = None
  for retry_num in range(num_retries + 1):
    if retry_num > 0:
      sleep(rand() * 2**retry_num)
      logging.warning(
          'Retry #%d for %s: %s %s%s' % (retry_num, req_type, method, uri,
          ', following status: %d' % resp.status if resp else ''))

    try:
      resp, content = http.request(uri, method, *args, **kwargs)
    except ssl.SSLError:
      if retry_num == num_retries:
        raise
      else:
        continue
    if resp.status < 500:
      break

  return resp, content
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号