python - 失败时如何重试 urllib2.request?

标签 python decorator urllib2 urllib3

urllib2.request 超时时,会引发 urllib2.URLError 异常。 重试建立连接的pythonic方法是什么?

最佳答案

我会使用 retry装饰师。那里还有其他的,但是这个很好用。使用方法如下:

@retry(urllib2.URLError, tries=4, delay=3, backoff=2)
def urlopen_with_retry():
    return urllib2.urlopen("http://example.com")

如果引发 URLError,这将重试该函数。检查上面的链接以获取有关参数的文档,但基本上它将重试最多 4 次,每次指数退避延迟加倍,例如3 秒、6 秒、12 秒。

关于python - 失败时如何重试 urllib2.request?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9446387/

相关文章:

python - Biopython 支持 Python 3.2 吗?

c# - 如何使用 CaSTLe Windsor 注册通用装饰器?

python - 在没有 http ://的情况下修复 url 的正确方法

python - Tensorflow 的计算时间在非常简单的 "for loop"中逐渐变慢

python - 在 Python 中的文件末尾声明函数

python - AWS Lambda-API 网关 "message": "Internal server error" (502 Bad Gateway)

python - 修饰的可调用类上的属性访问

javascript - 在 Typescript 中输入可选的可调用装饰器

python - 超时后 urlopen 调用不会在超时后终止

python - 从变量 Python (urllib2.urlopen) + Beautifulsoup4 打开链接