小编Sra*_*nda的帖子

重试 python requests 模块挂起

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

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)
Run Code Online (Sandbox Code Playgroud)

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

难道我做错了什么?

我正在使用 python 2.7.15rc1。

python python-2.7 python-requests

1
推荐指数
1
解决办法
1005
查看次数

标签 统计

python ×1

python-2.7 ×1

python-requests ×1