Flask初版架构源码解读 | Python基础

简介:

WSGI是什么?

WSGIWeb Server Gateway Interface
是基于现存的CGI标准而设计的,是PythonCGI进行的一种包装
也是一个规范,定义了Web服务器如何与Python应用程序进行交互,使得使用Python写的Web应用程序可以和Web服务器对接起来
目前DjangoFlask等主流Python Web框架都实现了WSGI

WSGI Web架构

image-20210618101348305.png

对于一个遵守WSGI协议的服务器和Web应用来说, 它并不在意到底是谁传过来的数据, 只需要知道传过来的数据符合某种格式, 两边都能处理对方传入的数据,具体交互流程如下:

  • 浏览器作为用户代理为我们发送了HTTP请求
  • 请求网络转发找到对应的处理服务器
  • HTTP服务器程序会将请求移交给Web应用处理该请求
  • Web应用处理完成,再将数据交给HTTP服务器软件
  • HTTP服务程序返回最终结果给浏览器
  • 浏览器接受到响应并展示给用户

Flask简介

Flask是一个轻量级的WSGI Web应用程序框架。它旨在使入门快速而简单,并能够扩展到复杂的应用程序。它最初是围绕WerkzeugJinja的一个简单包装器,现已成为最受欢迎的Python Web应用程序框架之一。

基于Flask我们可以很容易创建一个Web Applications,具体如下:

from flask import Flask, escape, request
app = Flask(__name__)

@app.route('/')
def hello():
    name = request.args.get("name", "World")
    return f'Hello, {escape(name)}!'
$ flask run
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

Flask源码解读

源码下载:git clone https://github.com/pallets/flask.git
为了剖析作者的最初架构设计,我们直接切换到初始分支:git checkout 0.1
下面我们看一个启动的具体Demo,并分析源码

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"    
app.run()

# * Running on http://localhost:5000/ (Press CTRL+C to quit)

初始化

def __init__(self, package_name):
    self.debug = False      # 是否打开Debug模式
    self.package_name = package_name    # 包名或模块名
    self.root_path = _get_package_path(self.package_name)   # app所在目录
    self.view_functions = {}    # 存储视图函数的词典,@route 装饰器进行注册
    self.error_handlers = {}    # 存储错误异常处理的字典,使用@errorhandler装饰器进行注册
    self.before_request_funcs = []  # 前置处理执行函数列表,使用@before_request装饰器进行注册
    self.after_request_funcs = []   # 后置处理执行函数列表,使用@after_request装饰器进行注册
    # 模版上下文
    self.template_context_processors = [_default_template_ctx_processor]
    self.url_map = Map()    # URL映射
    if self.static_path is not None:    # 静态文件
        self.url_map.add(Rule(self.static_path + '/<filename>', build_only=True, endpoint='static'))
        if pkg_resources is not None:
            target = (self.package_name, 'static')
        else:
            target = os.path.join(self.root_path, 'static')
        self.wsgi_app = SharedDataMiddleware(self.wsgi_app, {
            self.static_path: target
        })
    # 初始化 JinJa2 模版环境
    self.jinja_env = Environment(loader=self.create_jinja_loader(), **self.jinja_options)
    self.jinja_env.globals.update(url_for=url_for, get_flashed_messages=get_flashed_messages)

启动流程

Flask项目都是从app.run()入口启动,具体代码如下:

def run(self, host='localhost', port=5000, **options):
        """
        启动本地开发服务器,debug=True,代码热更部署标志
        :param host: 监听IP地址
        :param port: 监听端口,默认5000
        :param options: 运行相关的其他重要参数
        """
        from werkzeug import run_simple
        if 'debug' in options:
            self.debug = options.pop('debug')
        options.setdefault('use_reloader', self.debug)
        options.setdefault('use_debugger', self.debug)
        return run_simple(host, port, self, **options)

app.run()方法主要调用了werkzeug.run_simple进行服务启动,接下来我们具体看看run_simple的实现逻辑,具体代码如下:

def run_simple(hostname, port, application, use_reloader=False,
               use_debugger=False, use_evalex=True,
               extra_files=None, reloader_interval=1, threaded=False,
               processes=1, request_handler=None, static_files=None,
               passthrough_errors=False, ssl_context=None):
    if use_debugger:    # 是否使用werkzeug调试模式启动
        from werkzeug.debug import DebuggedApplication
        application = DebuggedApplication(application, use_evalex)
    if static_files:    # 静态文件封装为服务
        from werkzeug.wsgi import SharedDataMiddleware
        application = SharedDataMiddleware(application, static_files)

    def inner():    # 启动核心方法
        make_server(hostname, port, application, threaded, processes, request_handler, passthrough_errors, ssl_context).serve_forever()

    if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
        display_hostname = hostname != '*' and hostname or 'localhost'
        if ':' in display_hostname:
            display_hostname = '[%s]' % display_hostname
        _log('info', ' * Running on %s://%s:%d/', ssl_context is None and 'http' or 'https', display_hostname, port)
    if use_reloader:    # 服务是否热更新
        test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        test_socket.bind((hostname, port))
        test_socket.close()
        run_with_reloader(inner, extra_files, reloader_interval)
    else:
        inner()

werkzeug.run_simpleinner()方法封装了make_server()启动的核心逻辑,接下来我们具体看看make_server()的实现逻辑,具体代码如下:

def make_server(host, port, app=None, threaded=False, processes=1,
                request_handler=None, passthrough_errors=False,
                ssl_context=None):
    if threaded and processes > 1:
        raise ValueError("cannot have a multithreaded and multi process server.")
    elif threaded:
        return ThreadedWSGIServer(host, port, app, request_handler, passthrough_errors, ssl_context)
    elif processes > 1:
        return ForkingWSGIServer(host, port, app, processes, request_handler, passthrough_errors, ssl_context)
    else:
        return BaseWSGIServer(host, port, app, request_handler,  passthrough_errors, ssl_context)

make_server()函数内会根据线程或进程的数创建对应的WSGI服务器。默认情况下是创建BaseWSGIServer服务。接下来我们看看BaseWSGIServerThreadedWSGIServerForkingWSGIServer实现逻辑,具体代码如下:

class BaseWSGIServer(HTTPServer, object):
    """Simple single-threaded, single-process WSGI server."""
    multithread = False
    multiprocess = False
    ...
            
class ThreadedWSGIServer(ThreadingMixIn, BaseWSGIServer):
    """A WSGI server that does threading."""
    multithread = True

class ForkingWSGIServer(ForkingMixIn, BaseWSGIServer):
    """A WSGI server that does forking."""
    multiprocess = True
    ...

三个服务类继承关系如下:

image-20210618144659980.png

打开BaseWSGIServerstart_server()方法

def serve_forever(self):
    try:
        HTTPServer.serve_forever(self)
       except KeyboardInterrupt:
        pass

可以看到,整个启动流程最终是使用Python标准类库中的HTTPServer类接口,HTTPServersocketserver.TCPServer的子类,如果想要直接使用Python中类库启动一个http server,则可实现如下类似代码:

# -*- coding: utf-8 -
try:
    # Python2 逻辑
    import BaseHTTPServer
    import CGIHTTPServer
    server = BaseHTTPServer.HTTPServer(('127.0.0.1', 8000), CGIHTTPServer.CGIHTTPRequestHandler)
except:
    # Python3 逻辑
    from http import server
    server = server.HTTPServer(('127.0.0.1', 8000), server.CGIHTTPRequestHandler)
server.serve_forever()

至此,服务就启动起来了,整个流程如下:
image-20210618150202705.png

路由注册

@app.route('/')
def index():
    pass

以上是一个Flask注册的Demo,路由都是通过@app.route装饰器实现URL和视图函数的注册,具体代码如下:

def route(self, rule, **options):
    def decorator(f):
        self.add_url_rule(rule, f.__name__, **options)
        self.view_functions[f.__name__] = f
        return f
    return decorator
def add_url_rule(self, rule, endpoint, **options):
    options['endpoint'] = endpoint
    options.setdefault('methods', ('GET',))
    self.url_map.add(Rule(rule, **options))

模版渲染

from flask import render_template

@app.route('/hello/')
def hello(name=None):
    return render_template('hello.html', name=name)
def render_template(template_name, **context):
    current_app.update_template_context(context)
    return current_app.jinja_env.get_template(template_name).render(context)

这块逻辑比较简单,从templates文件夹中找到名称为template_name的文件进行渲染。其中current_appjinja_env都是在Flask.init()中被初始化

Werkzeug是什么?

werkzeug German noun: “tool”. Etymology: werk (“work”), zeug (“stuff”)

Werkzeug是一个综合的WSGI Web应用程序库。它最初是WSGI应用程序的各种实用程序的简单集合,现已成为最先进的 WSGI 实用程序库之一。

基于Werkzeug我们可以很容易创建一个WSGI Applications,具体如下:

from werkzeug.wrappers import Request, Response

@Request.application
def application(request):
    return Response("Hello, World!")

if __name__ == "__main__":
    from werkzeug.serving import run_simple
    run_simple("localhost", 5000, application)

Jinja是什么?

Jinja是一个快速、富有表现力、可扩展的模板引擎。模板中的特殊占位符允许编写类似于Python语法的代码,然后向模板传递数据以呈现最终文档。

接下来我们看一个使用模版渲染的例子

from flask import render_template

@app.route('/hello/')
def hello(name=None):
    return render_template('hello.html', name=name)

项目根目录下创建一个templates目录,并创建一个hello.html文件

/templates
    /hello.html

hello.html具体内容如下:

<!doctype html>
<title>Hello Web Flask</title>
{% if name %}
  <h1>Hello {{ name }}!</h1>
{% else %}
  <h1>Hello, Hello!</h1>
{% endif %}

其中name是参数,通过render_template方法就实现hello.html模版文件的渲染。

总结

Flask早期版本封装了werkzeugJinja函数库,以装饰器的方式对外提供WEB框架服务,相对更简单简洁。整个服务流程围绕Flask.run()方法启动服务开始。

参考文献

❤️❤️❤️读者每一份热爱都是笔者前进的动力!

我是李三十一,感谢各位朋友:点赞、收藏和评论,我们下期再见!

相关文章
|
3月前
|
人工智能 数据安全/隐私保护 异构计算
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
447 8
桌面版exe安装和Python命令行安装2种方法详细讲解图片去水印AI源码私有化部署Lama-Cleaner安装使用方法-优雅草卓伊凡
|
6月前
|
机器学习/深度学习 监控 算法
基于mediapipe深度学习的手势数字识别系统python源码
本内容涵盖手势识别算法的相关资料,包括:1. 算法运行效果预览(无水印完整程序);2. 软件版本与配置环境说明,提供Python运行环境安装步骤;3. 部分核心代码,完整版含中文注释及操作视频;4. 算法理论概述,详解Mediapipe框架在手势识别中的应用。Mediapipe采用模块化设计,包含Calculator Graph、Packet和Subgraph等核心组件,支持实时处理任务,广泛应用于虚拟现实、智能监控等领域。
|
3月前
|
设计模式 人工智能 API
AI智能体开发实战:17种核心架构模式详解与Python代码实现
本文系统解析17种智能体架构设计模式,涵盖多智能体协作、思维树、反思优化与工具调用等核心范式,结合LangChain与LangGraph实现代码工作流,并通过真实案例验证效果,助力构建高效AI系统。
501 7
|
4月前
|
机器学习/深度学习 算法 文件存储
神经架构搜索NAS详解:三种核心算法原理与Python实战代码
神经架构搜索(NAS)正被广泛应用于大模型及语言/视觉模型设计,如LangVision-LoRA-NAS、Jet-Nemotron等。本文回顾NAS核心技术,解析其自动化设计原理,探讨强化学习、进化算法与梯度方法的应用与差异,揭示NAS在大模型时代的潜力与挑战。
1055 6
神经架构搜索NAS详解:三种核心算法原理与Python实战代码
|
3月前
|
机器学习/深度学习 数据采集 算法
基于mediapipe深度学习的运动人体姿态提取系统python源码
本内容介绍了基于Mediapipe的人体姿态提取算法。包含算法运行效果图、软件版本说明、核心代码及详细理论解析。Mediapipe通过预训练模型检测人体关键点,并利用部分亲和场(PAFs)构建姿态骨架,具有模块化架构,支持高效灵活的数据处理流程。
|
3月前
|
小程序 PHP 图形学
热门小游戏源码(Python+PHP)下载-微信小程序游戏源码Unity发实战指南​
本文详解如何结合Python、PHP与Unity开发并部署小游戏至微信小程序。涵盖技术选型、Pygame实战、PHP后端对接、Unity转换适配及性能优化,提供从原型到发布的完整指南,助力开发者快速上手并发布游戏。
|
5月前
|
算法 数据可视化 数据挖掘
基于EM期望最大化算法的GMM参数估计与三维数据分类系统python源码
本内容展示了基于EM算法的高斯混合模型(GMM)聚类实现,包含完整Python代码、运行效果图及理论解析。程序使用三维数据进行演示,涵盖误差计算、模型参数更新、结果可视化等关键步骤,并附有详细注释与操作视频,适合学习EM算法与GMM模型的原理及应用。
|
5月前
|
API 数据安全/隐私保护 开发者
企业微信自动加好友软件,导入手机号批量添加微信好友,python版本源码分享
代码展示了企业微信官方API的合规使用方式,包括获取access_token、查询部门列表和创建用户等功能
|
4月前
|
并行计算 算法 Java
Python3解释器深度解析与实战教程:从源码到性能优化的全路径探索
Python解释器不止CPython,还包括PyPy、MicroPython、GraalVM等,各具特色,适用于不同场景。本文深入解析Python解释器的工作原理、内存管理机制、GIL限制及其优化策略,并介绍性能调优工具链及未来发展方向,助力开发者提升Python应用性能。
297 0
|
5月前
|
运维 安全 数据可视化
采用PHP+Vue技术架构的不良事件管理系统(源码)
本系统为医院安全(不良)事件管理工具,支持快速上报、流程化处理与多维度分析,助力识别风险、优化管理。采用PHP+Vue技术架构,功能涵盖事件上报、追踪整改、数据统计及PDCA改进等。
226 0

推荐镜像

更多