python实现requests重试机制

这篇博客介绍了如何在Python中使用requests库实现请求的重试机制,特别是在Python 2.7环境下解决因类型错误导致的问题。文章通过分析urllib3的递归实现,并给出了相关参考资料,鼓励读者深入理解技术原理,扎实学习,为技术生涯打下坚实基础。
摘要由CSDN通过智能技术生成

个人博客导航页(点击右侧链接即可打开个人博客):大牛带你入门技术栈 

一句话解决方案

import requests
from requests.adapters import HTTPAdapter
 
s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=3))
s.mount('https://', HTTPAdapter(max_retries=3))
 
s.get('http://example.com', timeout=5)

另外一种方式,深度定制, 但是我尝试了在python2.7的版本下不可以, 因为max_retries传入的参数是整型的, 如果传入Retry对象的话, 会报错:total -= 1 TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

def retry_test():
    s = requests.Session()

    retries = Retry(total=5,
                    backoff_factor=6,
                    status_forcelist=[500, 502, 503, 504])

    s.mount('http://', HTTPAdapter(max_retries=retries))

    resp = s.get('http://localhost:8088', timeout=3)
    print resp.text

原理

urllib3.connectionpool.py里, urlopen通过递归的方式实现

  def urlopen(
        self,
        method,
        url,
        body=None,
        headers=None,
        retries=None,
        redirect=True,
        assert_same_host=True,
        timeout=_Default,
        pool_timeout=None,
        release_conn=None,
        chunked=False,
        body_pos=None,
        **response_kw
    )
        """remove some codes"""

        try:
            # Request a connection from the queue.
            timeout_obj = self._get_timeout(timeout)
            conn = self._get_conn(timeout=pool_timeout)

            conn.timeout = timeout_obj.connect_timeout

            is_new_proxy_conn = self.proxy is not None and not getattr(
                conn, "sock", None
            )
            if is_new_proxy_conn:
                self._prepare_proxy(conn)

            # Make the request on the httplib connection object.
            httplib_response = self._make_request(
                conn,
                method,
                url,
                timeout=timeout_obj,
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值