【问题标题】:Retry for python requests module hanging重试python请求模块挂起
【发布时间】:2019-06-29 23:27:44
【问题描述】:

我不得不使用请求模块来获取大量 URL。由于网络错误,我想实现Retry机制。所以我的代码如下所示:

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
import requests

session = requests.Session()
retry = Retry(total=1,backoff_factor=0.5,status_forcelist=(500,502,504,503))
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://',adapter)        
head_response = session.get(url,timeout=2)

但是,当 URL 为:tiffins.NET 时,代码会一直挂起。 timeout=2 的正常 requests.get 会给出 503 状态码,但不会挂起。

我做错了吗?

我正在使用 python 2.7.15rc1。

【问题讨论】:

    标签: python python-2.7 python-requests


    【解决方案1】:

    刚遇到同样的问题,发现是因为网站的标题“Retry-After”。默认情况下,Retry 使用该值等待下一个请求。要忽略它,请在创建 Retry 对象时将respect_retry_after_header 参数设置为False

    【讨论】:

    • 今天这完全救了我!非常感谢!
    • 也救了我。 OP显然忘了接受这个答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 2014-09-12
    • 2013-05-10
    • 2014-01-14
    • 2020-06-02
    • 2023-03-24
    • 2022-12-12
    相关资源
    最近更新 更多