• 【Python百宝箱】掌握Python Web开发三剑客:Flask、Django、FastAPI一网打尽


    前言

    在当今互联网时代,Web应用的开发变得愈发重要和复杂。选择一个合适的Web框架,掌握安全性与认证、数据库与ORM库、前端框架与交互、测试与调试工具等关键知识点,是每个Web开发者都必须面对的挑战。本文将带你深入了解三个流行的Python Web框架(Flask、Django、FastAPI)及相关技术,涵盖全栈开发所需的关键知识,旨在助力开发者构建高效、安全、可扩展的Web应用。

    文章目录

    1. Flask

    1.1 简介与核心思想

    Flask是一个轻量级的Python Web框架,它以简单、灵活为设计理念,使得开发Web应用变得简单而快捷。以下是一个基本的Flask应用:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello, World!'
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1.2 路由和视图函数
    1.2.1 URL规则与变量

    Flask使用路由定义URL规则,可以包含变量。例如:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/user/')
    def show_user_profile(username):
        return 'User %s' % username
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1.2.2 请求与响应处理

    处理不同HTTP请求的方式:

    from flask import Flask, request
    
    app = Flask(__name__)
    
    @app.route('/login', methods=['GET', 'POST'])
    def login():
        if request.method == 'POST':
            # 处理登录逻辑
            return 'Login'
        else:
            return 'Show login form'
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1.3 模板引擎与前端集成
    1.3.1 Jinja2模板语言

    使用Jinja2模板引擎渲染动态内容:

    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    @app.route('/hello/')
    def hello(name=None):
        return render_template('hello.html', name=name)
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1.3.2 静态文件管理

    在Flask中处理静态文件(如CSS、JavaScript):

    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    @app.route('/static-example')
    def static_example():
        return render_template('static_example.html')
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1.4 扩展模块与插件
    1.4.1 Flask-SQLAlchemy

    使用Flask-SQLAlchemy进行数据库操作:

    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    
    app = Flask(__name__)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:tmp/test.db'
    db = SQLAlchemy(app)
    
    class User(db.Model):
        id = db.Column(db.Integer, primary_key=True)
        username = db.Column(db.String(80), unique=True, nullable=False)
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    1.4.2 Flask-RESTful

    使用Flask-RESTful创建RESTful API:

    from flask import Flask
    from flask_restful import Resource, Api
    
    app = Flask(__name__)
    api = Api(app)
    
    class HelloWorld(Resource):
        def get(self):
            return {'hello': 'world'}
    
    api.add_resource(HelloWorld, '/')
    
    if __name__ == '__main__':
        app.run()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    这是一个简单的开始,每个部分都可以进一步扩展和详细说明。

    2. Django

    2.1 简介与全功能性

    Django是一个高级、全功能的Web框架,提供了许多内置功能,使得开发大型Web应用更加便捷。以下是一个基本的Django应用:

    # mysite/settings.py
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
    
    # mysite/views.py
    from django.http import HttpResponse
    
    def hello(request):
        return HttpResponse("Hello, World!")
    
    # mysite/urls.py
    from django.urls import path
    from .views import hello
    
    urlpatterns = [
        path('hello/', hello),
    ]
    
    # mysite/urls.py
    from django.contrib import admin
    from django.urls import include, path
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('myapp/', include('myapp.urls')),
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    2.2 MVC架构与应用设计
    2.2.1 模型层(Models)

    定义Django模型:

    from django.db import models
    
    class Person(models.Model):
        first_name = models.CharField(max_length=30)
        last_name = models.CharField(max_length=30)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    2.2.2 视图层(Views)

    编写Django视图:

    from django.http import HttpResponse
    from .models import Person
    
    def person_list(request):
        people = Person.objects.all()
        return HttpResponse(', '.join([person.first_name for person in people]))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    2.2.3 控制层(Controllers)

    Django中的控制层由URL配置和视图组成。

    2.3 Admin后台管理系统
    2.3.1 数据库管理与界面定制

    Django的Admin界面自动生成,可以通过模型定义轻松管理数据。

    2.3.2 权限控制与用户管理

    Django提供了强大的用户管理系统,包括权限控制、用户认证等。

    2.4 Django REST framework
    2.4.1 序列化与视图类

    使用Django REST framework创建API视图:

    from rest_framework import serializers, viewsets
    from .models import Person
    
    class PersonSerializer(serializers.ModelSerializer):
        class Meta:
            model = Person
            fields = '__all__'
    
    class PersonViewSet(viewsets.ModelViewSet):
        queryset = Person.objects.all()
        serializer_class = PersonSerializer
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    2.4.2 认证与权限控制

    在Django REST framework中配置认证和权限:

    # settings.py
    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': [
            'rest_framework.authentication.BasicAuthentication',
            'rest_framework.authentication.SessionAuthentication',
        ],
        'DEFAULT_PERMISSION_CLASSES': [
            'rest_framework.permissions.IsAuthenticated',
        ],
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. FastAPI

    3.1 简介与性能优势

    FastAPI是一个现代、快速(基于Starlette和Pydantic)的Web框架,专注于API开发。

    3.2 基于类型提示的API开发
    3.2.1 请求与响应模型

    使用Pydantic模型定义请求与响应:

    from fastapi import FastAPI
    from pydantic import BaseModel
    
    app = FastAPI()
    
    class Item(BaseModel):
        name: str
        description: str = None
    
    @app.post("/items/")
    async def create_item(item: Item):
        return item
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    3.2.2 自动生成文档与交互式界面

    FastAPI自动生成交互式文档:

    from fastapi import FastAPI
    from fastapi.openapi.models import OAuthFlows as OAuthFlowsModel
    from fastapi.openapi.models import OAuthFlowAuthorizationCode
    
    app = FastAPI()
    
    @app.get("/items/")
    async def read_item():
        return {"name": "Example Item"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    3.3 异步支持与性能优化
    3.3.1 异步请求处理

    FastAPI支持异步请求处理:

    from fastapi import FastAPI
    
    app = FastAPI()
    
    @app.get("/")
    async def read_root():
        return {"message": "Hello, World!"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    3.3.2 高性能的依赖注入系统

    FastAPI的依赖注入系统:

    from fastapi import Depends, FastAPI, HTTPException
    
    app = FastAPI()
    
    async def get_query_parameter(q: str = None):
        if q:
            return {"q": q}
        return {"q": None}
    
    @app.get("/items/")
    async def read_item(commons: dict = Depends(get_query_parameter)):
        return commons
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    这是对FlaskDjangoFastAPI部分的初步填充。如果你有特定的问题或需要更详细的解释,请告诉我,我将进一步提供帮助。

    4. 数据库与ORM库

    4.1 SQLAlchemy
    4.1.1 ORM概述

    SQLAlchemy是一个强大的Python SQL工具和对象关系映射(ORM)库:

    from sqlalchemy import create_engine, Column, Integer, String, Sequence
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class User(Base):
        __tablename__ = 'users'
        id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
        name = Column(String(50))
        age = Column(Integer)
    
    engine = create_engine('sqlite:///:memory:')
    Base.metadata.create_all(engine)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    4.1.2 查询语言与表达式

    使用SQLAlchemy进行查询:

    from sqlalchemy.orm import sessionmaker
    
    Session = sessionmaker(bind=engine)
    session = Session()
    
    # 查询所有用户
    users = session.query(User).all()
    
    # 条件查询
    young_users = session.query(User).filter(User.age < 30).all()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    4.1.3 数据库迁移与版本控制

    使用Alembic进行数据库迁移:

    $ alembic init mymigration
    
    • 1
    4.2 Django ORM
    4.2.1 模型定义与关系

    在Django中定义模型及关系:

    from django.db import models
    
    class Author(models.Model):
        name = models.CharField(max_length=100)
    
    class Book(models.Model):
        title = models.CharField(max_length=200)
        author = models.ForeignKey(Author, on_delete=models.CASCADE)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    4.2.2 查询集与管理器

    Django中的查询:

    # 查询所有图书
    books = Book.objects.all()
    
    # 条件查询
    recent_books = Book.objects.filter(pub_date__gte='2020-01-01')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    4.2.3 数据迁移工具

    使用Django的makemigrationsmigrate进行数据迁移:

    $ python manage.py makemigrations
    $ python manage.py migrate
    
    • 1
    • 2

    5. 安全性与认证

    5.1 Flask-Security / Django-Allauth
    5.1.1 用户认证与授权

    实现用户注册与登录流程:

    • Flask-Security:
    from flask import Flask
    from flask_security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin
    
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'supersecretkey'
    
    # 用户数据模型
    class User(db.Model, UserMixin):
        # ...
    
    # 角色数据模型
    class Role(db.Model, RoleMixin):
        # ...
    
    # 数据库关联
    user_datastore = SQLAlchemyUserDatastore(db, User, Role)
    security = Security(app, user_datastore)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • Django-Allauth:
    # settings.py
    INSTALLED_APPS = [
        # ...
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        'allauth.socialaccount.providers.google',
    ]
    
    # urls.py
    urlpatterns = [
        # ...
        path('accounts/', include('allauth.urls')),
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    5.1.2 安全最佳实践

    实施密码存储与加密:

    • Flask-Security:
    # 在用户模型中使用密码哈希
    class User(db.Model, UserMixin):
        password = db.Column(db.String(255))
    
    • 1
    • 2
    • 3
    • Django-Allauth:

    默认使用PBKDF2算法进行密码哈希。

    5.2 python-jose / PyJWT
    5.2.1 JSON Web Token(JWT)认证

    JWT的结构与使用:

    # 使用python-jose进行JWT签名
    from jose import JWTError, jwt
    
    # 生成JWT
    def create_jwt(data: dict, secret_key: str, algorithm: str = "HS256") -> str:
        return jwt.encode(data, secret_key, algorithm=algorithm)
    
    # 解码JWT
    def decode_jwt(token: str, secret_key: str, algorithms: list[str] = ["HS256"]) -> dict:
        return jwt.decode(token, secret_key, algorithms=algorithms)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    5.2.2 Token生成与验证
    # 使用PyJWT生成和验证Token
    import jwt
    from datetime import datetime, timedelta
    
    # 生成Token
    def generate_token(user_id):
        payload = {
            'exp': datetime.utcnow() + timedelta(days=1),
            'iat': datetime.utcnow(),
            'sub': user_id
        }
        token = jwt.encode(payload, 'your-secret-key', algorithm='HS256')
        return token
    
    # 验证Token
    def verify_token(token):
        try:
            payload = jwt.decode(token, 'your-secret-key', algorithms=['HS256'])
            return payload
        except jwt.ExpiredSignatureError:
            return 'Signature expired. Please log in again.'
        except jwt.InvalidTokenError:
            return 'Invalid token. Please log in again.'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    6. 前端框架与交互

    6.1 React / Vue.js
    6.1.1 单页面应用(SPA)与前后端分离

    SPA的优势与劣势:

    • React:
    // React组件
    import React from 'react';
    
    function App() {
        return (
            

    Hello, React!

    ); } export default App;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • Vue.js:
    
    <template>
      <div>
        <h1>Hello, Vue.js!h1>
      div>
    template>
    
    <script>
    export default {
      // ...
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    6.1.2 组件化开发与状态管理

    组件的生命周期与通信:

    • React:
    import React, { useState, useEffect } from 'react';
    
    function Counter() {
        const [count, setCount] = useState(0);
    
        useEffect(() => {
            document.title = `Count: ${count}`;
        }, [count]);
    
        return (
            

    Count: {count}

    ); } export default Counter;
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • Vue.js:
    <template>
      <div>
        <p>Count: {{ count }}p>
        <button @click="increment">Incrementbutton>
      div>
    template>
    
    <script>
    export default {
      data() {
        return {
          count: 0
        };
      },
      methods: {
        increment() {
          this.count++;
        }
      }
    };
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    6.2 axios / fetch
    6.2.1 前端HTTP请求库的选择

    选择axiosfetch作为前端HTTP请求库:

    • axios:
    // 安装axios:npm install axios
    import axios from 'axios';
    
    // 发送GET请求
    axios.get('/api/data')
        .then(response => {
            console.log(response.data);
        })
        .catch(error => {
            console.error(error);
        });
    
    // 发送POST请求
    axios.post('/api/data', { data: 'example' })
        .then(response => {
            console.log(response.data);
        })
        .catch(error => {
            console.error(error);
        });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • fetch:
    // 使用fetch发送GET请求
    fetch('/api/data')
        .then(response => response.json())
        .then(data => console.log(data))
        .catch(error => console.error(error));
    
    // 使用fetch发送POST请求
    fetch('/api/data', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ data: 'example' })
    })
        .then(response => response.json())
        .then(data => console.log(data))
        .catch(error => console.error(error));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    6.2.2 异步数据交互与处理

    处理异步请求的数据交互与展示:

    • axios:
    // 使用axios进行异步数据交互
    async function fetchData() {
        try {
            const response = await axios.get('/api/data');
            console.log(response.data);
        } catch (error) {
            console.error(error);
        }
    }
    
    // 使用axios发送异步POST请求
    async function postData() {
        try {
            const response = await axios.post('/api/data', { data: 'example' });
            console.log(response.data);
        } catch (error) {
            console.error(error);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • fetch:
    // 使用fetch进行异步数据交互
    async function fetchData() {
        try {
            const response = await fetch('/api/data');
            const data = await response.json();
            console.log(data);
        } catch (error) {
            console.error(error);
        }
    }
    
    // 使用fetch发送异步POST请求
    async function postData() {
        try {
            const response = await fetch('/api/data', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ data: 'example' })
            });
            const data = await response.json();
            console.log(data);
        } catch (error) {
            console.error(error);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    7. 测试与调试工具

    7.1 pytest / unittest
    7.1.1 单元测试与测试用例

    使用pytestunittest进行单元测试:

    • pytest:
    # 安装pytest:pip install pytest
    # 编写测试文件 test_example.py
    def add(a, b):
        return a + b
    
    def test_add():
        assert add(1, 2) == 3
        assert add(0, 0) == 0
        assert add(-1, 1) == 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    运行测试:

    $ pytest test_example.py
    
    • 1
    • unittest:
    # 编写测试文件 test_example_unittest.py
    import unittest
    
    def add(a, b):
        return a + b
    
    class TestAddFunction(unittest.TestCase):
        def test_add(self):
            self.assertEqual(add(1, 2), 3)
            self.assertEqual(add(0, 0), 0)
            self.assertEqual(add(-1, 1), 0)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行测试:

    $ python -m unittest test_example_unittest.py
    
    • 1
    7.1.2 参数化测试与断言

    使用pytest的参数化测试:

    import pytest
    
    def add(a, b):
        return a + b
    
    @pytest.mark.parametrize("input_a, input_b, expected", [
        (1, 2, 3),
        (0, 0, 0),
        (-1, 1, 0),
    ])
    def test_add(input_a, input_b, expected):
        assert add(input_a, input_b) == expected
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    7.2 django-debug-toolbar / Flask Debug Toolbar
    7.2.1 性能监控与调试信息

    使用django-debug-toolbarFlask Debug Toolbar监控性能与调试信息:

    • django-debug-toolbar:
    # 安装django-debug-toolbar:pip install django-debug-toolbar
    # settings.py 中配置
    INSTALLED_APPS = [
        # ...
        'debug_toolbar',
    ]
    
    MIDDLEWARE = [
        # ...
        'debug_toolbar.middleware.DebugToolbarMiddleware',
    ]
    
    # urls.py 中配置
    if settings.DEBUG:
        import debug_toolbar
        urlpatterns = [
            path('__debug__/', include(debug_toolbar.urls)),
            # ...
        ] + urlpatterns
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • Flask Debug Toolbar:
    # 安装Flask Debug Toolbar:pip install Flask-DebugToolbar
    from flask import Flask
    from flask_debugtoolbar import DebugToolbarExtension
    
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'supersecretkey'
    toolbar = DebugToolbarExtension(app)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    8. 部署与扩展

    8.1 Docker / Docker Compose
    8.1.1 Docker容器化应用

    使用Docker将应用容器化:

    • Flask:
    # Dockerfile
    FROM python:3.8
    
    WORKDIR /app
    
    COPY requirements.txt requirements.txt
    
    RUN pip install -r requirements.txt
    
    COPY . .
    
    CMD ["python", "app.py"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    构建和运行容器:

    $ docker build -t my-flask-app .
    $ docker run -p 5000:5000 my-flask-app
    
    • 1
    • 2
    • Django:
    # Dockerfile
    FROM python:3.8
    
    WORKDIR /app
    
    COPY requirements.txt requirements.txt
    
    RUN pip install -r requirements.txt
    
    COPY . .
    
    CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    构建和运行容器:

    $ docker build -t my-django-app .
    $ docker run -p 8000:8000 my-django-app
    
    • 1
    • 2
    8.1.2 使用Docker Compose管理多个容器

    使用Docker Compose管理多个服务:

    # docker-compose.yml
    version: '3'
    
    services:
      flask-app:
        build: .
        ports:
          - "5000:5000"
    
      django-app:
        build: .
        command: python manage.py runserver 0.0.0.0:8000
        ports:
          - "8000:8000"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    启动服务:

    $ docker-compose up
    
    • 1
    8.2 Nginx / Gunicorn
    8.2.1 使用Nginx作为反向代理

    将Nginx配置为Flask或Django应用的反向代理:

    • Flask:
    # Flask应用的Nginx配置
    server {
        listen 80;
        server_name my-flask-app.com;
    
        location / {
            proxy_pass http://127.0.0.1:5000;
            include /etc/nginx/proxy_params;
            proxy_redirect off;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • Django:
    # Django应用的Nginx配置
    server {
        listen 80;
        server_name my-django-app.com;
    
        location / {
            proxy_pass http://127.0.0.1:8000;
            include /etc/nginx/proxy_params;
            proxy_redirect off;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    8.2.2 使用Gunicorn作为应用服务器

    使用Gunicorn作为Flask或Django应用的应用服务器:

    • Flask:
    $ gunicorn -w 4 -b 127.0.0.1:5000 my_flask_app:app
    
    • 1
    • Django:
    $ gunicorn -w 4 -b 127.0.0.1:8000 my_django_app.wsgi:application
    
    • 1
    8.3 性能优化与缓存
    8.3.1 Memcached / Redis缓存

    使用Memcached或Redis进行缓存优化:

    • Flask:
    # Flask应用使用Memcached
    from flask import Flask
    from flask_caching import Cache
    
    app = Flask(__name__)
    cache = Cache(app, config={'CACHE_TYPE': 'memcached'})
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • Django:
    # Django应用使用Redis
    # settings.py
    CACHES = {
        'default': {
            'BACKEND': 'django_redis.cache.RedisCache',
            'LOCATION': 'redis://127.0.0.1:6379/1',
            'OPTIONS': {
                'CLIENT_CLASS': 'django_redis.client.DefaultClient',
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    8.3.2 异步任务与队列

    使用Celery进行异步任务处理:

    • Flask:
    # Flask应用使用Celery
    from flask import Flask
    from celery import Celery
    
    app = Flask(__name__)
    celery = Celery(app.name, broker='pyamqp://guest:guest@localhost//')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • Django:
    # Django应用使用Celery
    # settings.py
    CELERY_BROKER_URL = 'pyamqp://guest:guest@localhost//'
    
    • 1
    • 2
    • 3
    8.4 监控与日志
    8.4.1 使用Prometheus监控

    使用Prometheus进行监控:

    • Flask:
    # Flask应用使用Prometheus
    from flask import Flask
    from prometheus_flask_exporter import PrometheusMetrics
    
    app = Flask(__name__)
    metrics = PrometheusMetrics(app)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • Django:
    # Django应用使用Prometheus
    # settings.py
    INSTALLED_APPS = [
        # ...
        'django_prometheus',
    ]
    
    MIDDLEWARE = [
        # ...
        'django_prometheus.middleware.PrometheusBeforeMiddleware',
        # ...
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    8.4.2 集成ELK日志系统

    将应用日志集成到ELK(Elasticsearch, Logstash, Kibana)系统:

    • Flask:
    # Flask应用配置日志到ELK
    from flask import Flask
    import logging
    from logstash_async.handler import AsynchronousLogstashHandler
    
    app = Flask(__name__)
    
    # 配置ELK日志处理器
    handler = AsynchronousLogstashHandler(
        host='logstash-host',
        port=5959,
        database_path='async_logstash.db',
        ssl_enable=False,
    )
    
    # 添加处理器到应用
    app.logger.addHandler(handler)
    
    # 通过日志记录
    app.logger.info('This is an ELK log entry')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • Django:
    # Django应用配置日志到ELK
    # settings.py
    LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'handlers': {
            'logstash': {
                'level': 'DEBUG',
                'class': 'logstash.LogstashHandler',
                'host': 'logstash-host',
                'port': 5959,
                'database_path': 'logstash.db',
            },
        },
        'loggers': {
            'django': {
                'handlers': ['logstash'],
                'level': 'DEBUG',
                'propagate': True,
            },
        },
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    总结

    通过阅读本文,读者将深入了解Python Web开发的关键概念和实践方法。我们详细介绍了三种主流框架,以及它们的优势和适用场景。同时,我们涉及了数据库操作、ORM库的选择、安全性与认证、前端框架的使用、测试与调试工具,以及部署和性能优化等方面的内容。这篇文章的目标是为读者提供一份全面而深入的指南,使他们能够以更自信的姿态构建高效、安全、可维护的Web应用。

  • 相关阅读:
    同步异常、External abort、Serror、prefetch abort、data abort的区别
    基于LSTM算法中文歌词生成
    html+css鼠标悬停发光按钮![HTML鼠标悬停的代码]使用HTML + CSS实现鼠标悬停的一些奇幻效果!
    硬件开发笔记(十九):Altium Designer 21软件介绍和安装过程
    【数据库-黑马笔记】基础-SQL
    vue3 封装公共弹窗函数
    PTA乙级 1105 链表合并——25分
    git push : RPC failed; HTTP 400 curl 22 The requested URL returned error: 400
    如何看待Unity新的收费模式?
    SpringBean面试题
  • 原文地址:https://blog.csdn.net/qq_42531954/article/details/134520754