← CS Lab Programming Foundations / Python Web Frameworks
Python Web Frameworks — card 1 of 4
Python Web Frameworks — card 2 of 4
Python Web Frameworks — card 3 of 4
Python Web Frameworks — card 4 of 4

A web framework turns HTTP requests into Python function calls. You write functions; the framework figures out which one runs for a given URL and method, then ships whatever it returns back to the browser. Flask, Django and FastAPI are the three you'll meet most.

Where you meet it

What it does

It handles the boring, repetitive parts of talking HTTP: parsing the incoming request, matching it to your code, and packaging your return value as a proper response (status code, headers, body). You write the logic; it wires it to the web.

A framework is just routing plus glue — pick it for the job, and remember async only pays off when you actually await I/O.

How it works

The core idea is routing: the framework maps a URL + HTTP method to a handler function. The handler runs and returns a value, which becomes the response.

# Flask — a route, a handler, a response
from flask import Flask
app = Flask(__name__)

@app.route("/users/<int:id>")   # method (GET) + path
def get_user(id):                  # the handler
    return {"id": id}              # becomes a JSON response

The three frameworks make different bets:

Under the hood, frameworks plug into a web server through a standard interface. WSGI is the older, synchronous one (Flask, Django classically). ASGI is its async successor — it can handle long-lived connections like WebSockets, which WSGI's one-call-per-request design can't (FastAPI runs on ASGI).

Watch out

  • Pick the framework for the job, not the hype. A small HTML site doesn't need async; a high-concurrency API doesn't need Django's full stack.
  • Async isn't free. async only helps when you actually await I/O. Sync code in an async handler still blocks the worker.
  • The development server is not for production. Flask's built-in server and debugger run arbitrary code — deploy behind a real server (Gunicorn, Uvicorn) instead.

Go deeper

Ein Web-Framework verwandelt HTTP-Anfragen in Aufrufe von Python-Funktionen. Du schreibst Funktionen; das Framework entscheidet, welche für eine gegebene URL und Methode läuft, und schickt deren Rückgabewert zurück an den Browser. Flask, Django und FastAPI sind die drei, die dir am häufigsten begegnen.

Wo es vorkommt

Was es tut

Es übernimmt die langweiligen, sich wiederholenden Teile der HTTP-Kommunikation: die eingehende Anfrage parsen, sie deinem Code zuordnen und deinen Rückgabewert als saubere Response verpacken (Statuscode, Header, Body). Du schreibst die Logik; das Framework verdrahtet sie mit dem Web.

Ein Framework ist nur Routing plus Klebstoff — wähle es zur Aufgabe, und async zahlt sich nur aus, wenn du wirklich I/O awaitest.

Wie es funktioniert

Die Kernidee ist Routing: Das Framework bildet eine URL + HTTP-Methode auf eine Handler-Funktion ab. Der Handler läuft und gibt einen Wert zurück, der zur Response wird.

# Flask — Route, Handler, Response
from flask import Flask
app = Flask(__name__)

@app.route("/users/<int:id>")   # Methode (GET) + Pfad
def get_user(id):                  # der Handler
    return {"id": id}              # wird zur JSON-Response

Die drei Frameworks setzen auf Unterschiedliches:

Unter der Haube binden sich Frameworks über eine Standard-Schnittstelle an den Webserver. WSGI ist die ältere, synchrone (klassisch Flask, Django). ASGI ist ihr asynchroner Nachfolger — er kann langlebige Verbindungen wie WebSockets bedienen, was WSGIs Ein-Aufruf-pro-Anfrage-Design nicht kann (FastAPI läuft auf ASGI).

Worauf achten

  • Wähle das Framework zur Aufgabe, nicht zum Hype. Eine kleine HTML-Seite braucht kein async; eine API mit hoher Nebenläufigkeit braucht nicht Djangos vollen Stack.
  • Async ist nicht gratis. async hilft nur, wenn du tatsächlich I/O awaitest. Synchroner Code in einem async-Handler blockiert den Worker trotzdem.
  • Der Entwicklungs-Server ist nicht für Produktion. Flasks eingebauter Server und Debugger führen beliebigen Code aus — deploye hinter einem echten Server (Gunicorn, Uvicorn).

Mehr dazu

Next up
Databases & Data ModelingNoSQL & Document Databases AI & Machine LearningML Frameworks Ecosystem All concepts →