mirror of
https://github.com/Rainyy21/framework_note.git
synced 2026-10-11 01:10:28 -04:00
vault backup: 2026-06-01 23:17:01
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
---
|
||||
tags:
|
||||
- python
|
||||
- web-development
|
||||
- flask
|
||||
- wsgi
|
||||
created: 2026-06-01
|
||||
category: 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](https://peps.python.org/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_response` function).
|
||||
* Low-level: It doesn't handle routing, templating, or cookies directly; it just passes data back and forth.
|
||||
* **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`**
|
||||
|
||||
1. **Nginx** receives the HTTP request.
|
||||
2. **Gunicorn** (the WSGI Server) translates that HTTP request into the WSGI environment format.
|
||||
3. **Flask** receives the WSGI call, routes it to your function, and returns a response.
|
||||
4. **Gunicorn** translates that response back into HTTP for the user.
|
||||
Reference in New Issue
Block a user