mirror of
https://github.com/Rainyy21/framework_note.git
synced 2026-10-11 01:10:28 -04:00
2.7 KiB
2.7 KiB
tags, created, category
| tags | created | category | ||||
|---|---|---|---|---|---|---|
|
2026-06-01 | programming |
WSGI vs. Flask: Understanding the Relationship
In the Python web ecosystem, WSGI and Flask operate at different layers of the stack. Here is a breakdown of their differences and how they work together.
1. What is WSGI?
WSGI (Web Server Gateway Interface) is a specification, not a software or a framework. Defined in PEP 3333, it describes a standard interface between web servers (like Nginx or Apache) and Python web applications or frameworks.
- Role: It acts as a bridge. It ensures that any WSGI-compliant server can run any WSGI-compliant application.
- Key Characteristics:
- Simple interface: A callable object (usually a function or class) that accepts two arguments (the environment dictionary and a
start_responsefunction). - Low-level: It doesn't handle routing, templating, or cookies directly; it just passes data back and forth.
- Simple interface: A callable object (usually a function or class) that accepts two arguments (the environment dictionary and a
- Examples of WSGI Servers: Gunicorn, uWSGI, Waitress.
2. What is Flask?
Flask is a micro web framework built on top of the WSGI specification. It provides the tools, libraries, and technologies to build a web application.
- Role: It simplifies development by providing high-level abstractions for common tasks.
- Key Characteristics:
- Built-in routing (mapping URLs to Python functions).
- Template engine integration (Jinja2).
- Request/Response handling (cookies, sessions, headers).
- Development server (though not suitable for production).
- Dependency: Flask uses a library called Werkzeug, which is a comprehensive WSGI utility library.
3. Key Differences at a Glance
| Feature | WSGI | Flask |
|---|---|---|
| Type | Specification / Interface | Web Framework |
| Level | Low-level (system bridge) | High-level (application code) |
| Purpose | Standardization and compatibility | Rapid application development |
| Functionality | Passing data between server and app | Routing, templates, logic, session management |
| Relationship | Flask is an implementation of a WSGI app | Flask relies on WSGI to talk to the web server |
4. How They Work Together
In a typical production environment, the stack looks like this:
User Browser <-> Web Server (Nginx) <-> WSGI Server (Gunicorn) <-> Flask App
- Nginx receives the HTTP request.
- Gunicorn (the WSGI Server) translates that HTTP request into the WSGI environment format.
- Flask receives the WSGI call, routes it to your function, and returns a response.
- Gunicorn translates that response back into HTTP for the user.