Python - 等待远程系统启动的最佳方法

jaz*_*azz 5 python wait python-requests

我正在使用 LAN 唤醒在 python 脚本中启动某个服务器。

当我可以成功执行 API 请求时,服务器就在线,例如:

return requests.get(
        url + path,
        auth=('user', user_password),
        headers={'Content-Type':'application/json'},
        verify=False,
        timeout=0.05
    ).json()
Run Code Online (Sandbox Code Playgroud)

等待服务器启动过程(直到可以通过 API 访问)而不用循环请求向网络发送垃圾邮件的最佳方法是什么?

ais*_*baa 1

我相信你非常接近。为什么不将该请求放入while广告try except块中?

while True:
    try:
        return requests.head(...)
    except requests.exceptions.ConnectionError:
        time.sleep(0.5)
Run Code Online (Sandbox Code Playgroud)