python - python-requests 一次失败后如何再发送一个请求?

标签 python python-requests

我正在编写一个 url getter 。当我发送如下请求时:

import requests
response = requests.get("http://example.com")

有时会出现这样的错误:

ConnectionError: ('Connection aborted.', BadStatusLine(""''''")) 

但是当我再尝试一次时,问题就解决了。所以当再次出现这样的错误时我想再发送一次。我怎样才能做到这一点?预先感谢您!

最佳答案

当异常发生时再次重复请求。

import requests

url = "http://example.com"

try:
    response = requests.get(url)
except requests.exception.ConnectionError:
    response = requests.get(url)

关于python - python-requests 一次失败后如何再发送一个请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162673/

相关文章:

python - 将带有 json 的 numpy 数组发布到带有请求的 flask 应用程序

python - 必须在您的浏览器中启用 Cookie [Python 请求]

Python - curl 请求 - 'data-urlencode'

python - 尝试验证 Power Bi Web 应用程序时,重定向 url 不返回授权代码

python - 如果我在 python 中使用多处理但我的函数不返回任何内容,我是否需要调用 get()?

python - 从配置文件自定义 python 结构化 (json) 日志记录中的键值

python - 为什么 psychopy.visual.MovieStim3 在我的用例中这么慢?

python - 我对此类网站的 POST 感到困惑

python - d.update(dict(a=1, b=2)) 和 d.update(dict ('a' =1, 'b' =2)) 有什么区别

php - 可用于编程的最有值(value)的认证是什么?