【问题标题】:requests.post hangs and keeps on retrying in python3requests.post 挂起并继续在 python3 中重试
【发布时间】:2020-02-14 05:33:00
【问题描述】:

我正在为物联网设备编写一个 python 脚本,其中涉及将一些数据发布到 API。该设备正在与本地无线网络上的某个节点进行通信,并在 4G 蜂窝网络上运行。

现在,如果存在互联网连接,设备将按预期工作并发布数据。但是如果设备没有连接到互联网,requests.post 调用会卡住很长一段时间,而在理想情况下,它应该会引发 requests.ConnectionError 例外。脚本的以下部分尝试发布数据。

try:
   resp = requests.post(DATA_UPLOAD_API,json=postData,timeout=2)
except requests.ConnectionError as err:
   self.logger.info("Failed to post the data with the exception-" + str(err) + "....trying again")
   count=count+1
   pass

它是否与本地无线网络有关,即使它不在线,它也会不断重试发布?如果不是这样,可能是什么问题?

【问题讨论】:

    标签: python-3.x post python-requests


    【解决方案1】:

    那是因为代码抛出了异常,而不仅仅是ConnectionError

    用这个来捕获异常。

    except requests.exceptions.ConnectionError:
        print("No")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2011-11-16
      相关资源
      最近更新 更多