Interview Prep/Python for DevOps/Testing, services & metrics

How do you write a minimal health-check service with Flask?

BeginnerConceptTesting, services & metrics
Answer
from flask import Flask, jsonify
app = Flask(__name__)

@app.get("/healthz")
def healthz():
    return jsonify(status="ok")

@app.get("/readyz")
def readyz():
    ok = db_ping()
    return jsonify(db=ok), (200 if ok else 503)

Run it with a production server: gunicorn -w 2 -b 0.0.0.0:8000 app:app. flask run and app.run() start the Werkzeug development server, which prints a warning and is not meant for production.

All Python for DevOps questions Roadmap: Python for automation

More on Testing, services & metrics