Python3 requests 使用Addapter时,timeout失败

如题,最近在使用requests包进行一些RESTFul api的编程,有一部分涉及到retry的操作,requests包提供了非常方便的配置retry的方法,具体的网上教程很多,这里不做赘述,直接贴一下我自己的代码。

def create_session() -> requests.Session:
    retry_strategy = Retry(
        total=TeeControlClient._POST_RETRY_COUNT,
        status_forcelist=[429, 500, 502, 503, 504],
        method_whitelist=["POST"]
    )
    adapter = requests.adapters.HTTPAdapter(max_retries=retry_strategy)
    session = requests.Session()
    session.mount("https://", adapter)
    session.mount("http://", adapter)

    return session

我这个是专门给POST方法配置的,所以method_whitelist配置的只有一个POST。

这个配置看起来没啥问题,然后大家都知道requests的api都应该配置一个timeout,我有一个测试用例就是连接一个url,因为proxy的原因会卡住不动。具体代码如下


session = create_session()

try:
    return session.post(url, json=json_data, timeout=10)
except requests.exceptions.RequestException as error:
    logger.error("Cannot connect to %s, Reason: %s", url, error)

然后执行代码发现直接卡死,timeout没有起作用!

多方查证之后发现是因为这种因为我的case是因为网络原因,使用了Adapter之后,需要配置Retry的connect属性

connect (int) –

  How many connection-related errors to retry on.

  These are errors raised before the request is sent to the remote server, which we assume has not triggered the server to    process the request.

  Set to 0 to fail on the first retry of this type.

直接代码改成:

def create_session() -> requests.Session:
    """ create a Session with Retry Adapter

    Returns:
        Session with Retry Adapter
    """
    retry_strategy = Retry(
        total=TeeControlClient._POST_RETRY_COUNT,
        status_forcelist=[429, 500, 502, 503, 504],
        method_whitelist=["POST"],
        connect=0,
    )
    adapter = requests.adapters.HTTPAdapter(max_retries=retry_strategy)
    session = requests.Session()
    session.mount("https://", adapter)
    session.mount("http://", adapter)

    return session

问题搞定,retry和timeout都生效了

其他还有 read 等属性,可以参考一下 urllib3 ,里面搜索一下retry就能找到了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值