Use web framework
From native WSGI to modern framework (Flask practical introduction)
In the early days of Python Web development, we had to directly face the "barbed wire" of the HTTP protocol and write the underlying request parsing and response structure by hand. Today, various full-featured/micro-frameworks have packaged these dirty work cleanly. This article will take you through the process of "from wheels to sports cars" and use Flask to build a complete login project so that you can get started easily.
1. Everything starts with WSGI: a unified standard interface
Python can have so many excellent web frameworks, and it is inseparable from a key industry-level abstract specification - WSGI (Web Server Gateway Interface). It specifies the handshake method between "Web server (such as Nginx, Gunicorn)" and "Python application", so you no longer need to write separate adaptation code for each server.
What does the original WSGI application look like?
Just write a callable object (usually a function) that accepts two fixed parameters:
Although interaction has been unified, it is still a galaxy away from real project development:
- Routers need to parse the URL themselves and find the corresponding processing function.
- The request method (GET/POST) needs to be distinguished manually
- You have to process the byte stream yourself when taking form parameters.
- No basic capabilities such as template engine, database abstraction, and security protection
- Once the code is complex, maintaining it is like defusing bombs
**So the answer is clear: we need frameworks. **
2. What’s so popular about modern Python web frameworks?
The core idea of the framework is to replace "repeated handwritten underlying logic" with "conventions" or "configuration items" to help you stop dirty work and focus on business. It generally provides these "out of the box" capabilities:
- ✅ Auto routing: A decorator can tie the URL to the processing function
- ✅ Request/Response Encapsulation: Get parameters and set status codes directly through objects, no need to mess with native bytes
- ✅ Built-in template engine: decoupling HTML and Python code, making the front-end comfortable to read
- ✅ Security components: CSRF protection, password hashing, security headers, etc.
- ✅ Plug-in Ecosystem: Database, API, WebSocket, you can add whatever you want.
3. The first choice for lightweight entry: Flask framework in action
Flask is the most popular microframework in the Python community - the core is only responsible for the basic encapsulation of routing and template rendering. It is very small, but it can be transformed into a full-featured development kit through plug-ins. Perfect for getting started quickly, prototyping, or small projects.
3.1 Step 1: Install Flask
Open the terminal and do it with one line of command:
3.2 Step 2: Write a "minimum runnable" application
runpython app.py, open in the browserhttp://127.0.0.1:5000, you will see your first Web page!
3.3 Step 3: Process multiple HTTP methods + form data
Flask only responds to GET requests by default. If you want to support additional methods such as POST, you need to@app.routespecified inmethodsparameters, and userequestObject fetch form:
This way we have a route that can receive user input and respond differently.
3.4 Step 4: Advanced small project - login system with password hashing and Session
Although the above example works, there are three obvious problems:
- HTML is written directly in Python strings, making it difficult to maintain
- There is no login status and it will be lost as soon as it is refreshed.
- If the password is saved in clear text, it is equivalent to running naked.
Let's make a prototype that is in line with modern practice. We do not introduce additional databases, use memory dictionaries to simulate user data, and use the security tools that come with Flask and Werkzeug.
Built-in tools that will be used
session:Storage login statusflash: Pop up a prompt message on the next page (success/failure)generate_password_hashandcheck_password_hash: Secure password hashing and verification provided by Werkzeugrender_template_string: Render strings containing Jinja2 syntax into HTML (inline templates are used here, and it is recommended to separate them into template files for actual projects)
Complete code
Operation effect description
- First visit
/, see the "Go to Login" link. - Enter
/login, enter the wrong username/password, and the page will display a red error prompt. - Use the correct
test_user / test123456Log in, automatically jump back to the homepage, display the welcome message, and prompt "Login successful". - Click Exit to clear the login status and return to the non-login status again.
🧠 Tip: This example uses
render_template_stringRendering inline templates is to make the entire logic in one file for easy demonstration. Real projects recommend extracting HTML totemplates/directory, userender_template()load.
4. Quick overview of mainstream Python web frameworks
Different needs require different “weapons”. Here is a quick selection reference for you:
5. Summary and suggestions
- Don’t reinvent the wheel from native WSGI: The framework exists to allow you to write less low-level code.
- Select according to project scale: Choose Flask for small projects/rapid prototyping, choose Django for full-featured heavy-duty applications, and choose FastAPI for high real-time APIs.
- Always keep safety and practice in mind:
- Never hardcode
secret_keyand database password, use environment variables. - The password must be hashed (used here
pbkdf2:sha256It’s very safe). - Completely separate business logic, display templates, and configuration.
- Production environment deployment reminder:
app.run()It is just a development server. When going online, you must use Gunicorn / uWSGI with Nginx, or use Docker directly.
I hope this tutorial from underlying principles to hands-on practice can help you smoothly open the door to Python web development!

