get_json.py 文件源码

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

项目:talk-network-retries 作者: marchukov 项目源码 文件源码
def get_retry_extended():
    retry = urllib3.util.Retry(total=MAX_RETRIES, connect=MAX_RETRIES, read=MAX_RETRIES, backoff_factor=BACKOFF_FACTOR)

    def attempt(url, retry=retry):
        try:
            # this essentially creates a new connection pool per request :-(
            session = requests.Session()
            adapter = requests.adapters.HTTPAdapter(max_retries=retry)
            session.mount(RETRY_PREFIX, adapter)
            req = requests.Request('GET', url).prepare()
            # would be nice just to pass retry here, but we cannot :-(
            r = session.send(req, timeout=TIMEOUT)
            r.raise_for_status()
#        except MaxRetryError:
#            raise
        except ConnectionError as e:
            #  increment() will return a new Retry() object
            retry = retry.increment(req.method, url, error=e)
            retry.sleep()
            logging.warning("Retrying (%r) after connection broken by '%r': '%s'", retry, e, url)
            return attempt(url, retry=retry)
        return r

    return attempt(URL).json()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号