Search code examples
pythonpython-requestswait

Python - best way to wait until a remote system is booted


I am using wake on lan to start a certain server in a python script.

The server is online when I can do a successfull API request, such as:

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

What is the best method to wait for the server bootup process (until it is reachable via API) without spamming the network with requests in a loop?


Solution

  • I believe you're very close. Why not put that request in while a d try except blocks?

    while True:
        try:
            return requests.head(...)
        except requests.exceptions.ConnectionError:
            time.sleep(0.5)