是否有办法让机械化系统在请求失败时重新尝试?

2024-04-25 14:28:56 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法设置mechanize在HTTP请求因超时而失败时重试它?或者我需要将它放入一个无限循环中,并在请求成功时使它脱离循环?在


Tags: httpmechanize办法
1条回答
网友
1楼 · 发布于 2024-04-25 14:28:56

您需要自己测试超时异常。以下未经测试的代码显示了这一想法:

import mechanize
import socket
timeout_occurred = False
br = mechanize.Browser()
try:
    br.open("http://python.org/", timeout=0.001)
except mechanize.URLError as exc:
    if isinstance(exc.reason, socket.timeout):
        timeout_occurred = True
if timeout_occurred:
    # retry
    br.open("http://python.org/", timeout=0.001)

此外,您还需要阅读有关超时如何工作的常见问题解答。在

相关问题 更多 >