needmoreeasy EN 한국어
Learn

76 — Network: polling a server

★★★★★ (5/5)network & timing
Prerequisites
55 — Network, 70 — Reaction
You will end up with
repeatedly fetching a status.json from a local server every few seconds and reporting changes

Programs that watch a server keep asking it for the latest state. This is called polling. The loop is simple: fetch, show, wait, repeat — and stop when the server says it is done.

Steps

  1. Create status.json with a state that a worker will change:

    {"status": "working", "step": 1}
    
    run it →
  2. Serve the folder, as in guide 55:

    python3 -m http.server 8000
    
  3. Fetching the status is the same urlopen call as guide 55:

    from urllib.request import urlopen
    from json import loads
    
    url = "http://localhost:8000/status.json"
    status = loads(urlopen(url).read().decode("utf-8"))
    
    run it →
  4. The full program polls in a loop, reports each state, and stops when the server reports "done". Save poll.nme:

    # poll.nme — watch status.json until it says done.
    # Run: nme r poll
    # Serve this folder first: python3 -m http.server 8000
    # Change status.json to {"status": "done", "step": 2} while it runs.
    
    from urllib.request import urlopen
    from json import loads
    from time import sleep
    
    url = "http://localhost:8000/status.json"
    
    while True:
        status = loads(urlopen(url).read().decode("utf-8"))
        show f"step {status['step']}: {status['status']}"
        if status["status"] == "done":
            show "worker finished"
            break
        sleep(1)
    
    run it →

    Each loop fetches the current file, prints it, and waits one second. To see the change, edit status.json (the HTTP server re-reads the file on every request) and watch the program report the new step before stopping.

  5. Run it, then edit status.json while it runs:

    nme r poll
    
    step 1: working
    step 2: done
    worker finished
    

Try it yourself

Add a started timestamp to status.json and report how many seconds the worker ran, or poll two endpoints and report when either one finishes.

What you learned

  • Polling means fetching the latest state in a loop with a wait between tries.
  • The HTTP server re-reads the file on every request, so edits appear live.
  • A sentinel value like "done" tells the loop when to stop.
  • time.sleep controls how often the program asks.

This page on GitHub