Python

小巧又灵活的轻量级框架

Flask

  1. MVC设计思想
    • 客户端发起请求后通过路由找到视图处理函数
    • 路由和视图处理函数事先在app中声明
    • 在视图的处理函数中根据业务需求,加载数据并渲染到模板中
    • 将渲染之后的模板数据返回给客户端
  2. MTV设计思想
    • Model
    • Template
    • View
  3. 模板(html)
    • 默认必须存在templates模块
    • 与工作目录保持同级

代码示例:

from flask import Flask, request  # request:请求对象(包含了请求资源的路径、请求方法、请求头、上传的表单数据、文件等信息)

app = Flask(__name__)  # 创建flask对象(实参:Flask应用对象的名称,名称不可以是flask,否则会报错找不到模板)


@app.route('/index', methods=['GET', 'POST'])  # 声明请求资源路由与方式
def index():
    platform = request.args.get('platform', 'PC')  # 获取平台参数(仅支持PC访问)
    if platform.lower() == 'android':
        return '''
        <h1 style="color:red">仅支持PC端访问</h1>
        '''
    if request.method == 'GET':
        return '''
        <h1>Hello World!</h1>
        <form action="/index?platform=PC" method="post">
            <input name="name" placeholder="用户名"><br>
            <input name="password" placeholder="密码"><br>
            <button>提交</button>
        </form>
        '''
    else:  # 获取表单参数
        name = request.form.get('name')
        password = request.form.get('password')
        if all((name.strip() == 'username', password.strip() == 'password')):
            return '''
            <h1>登录成功!</h1>
            '''
        else:
            return '''
            <h1>登录失败!</h1>
            '''


if __name__ == '__main__':
    app.run('localhost', 80)  # 启动flask服务

使用模板

代码示例:

<!-- 模板(test.html) -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>test</title>
</head>
<body>
    <!-- 动态显示 -->
    <h1>{{title}}}的页面</h1>
    <span>{{span}}}</span>
</body>
</html>
from flask import Flask, request, render_template

app = Flask(__name__)


@app.route('/index', methods=['GET', 'POST'])
def index():
    platform = request.args.get('platform', 'PC')
    if platform.lower() == 'android':
        return '''
        <h1 style="color:red">仅支持PC端访问</h1>
        '''
    if request.method == 'GET':
        return '''
        <h1>Hello World!</h1>
        <form action="/index?platform=PC" method="post">
            <input name="name" placeholder="用户名"><br>
            <input name="password" placeholder="密码"><br>
            <button>提交</button>
        </form>
        '''
    else:
        name = request.form.get('name')
        password = request.form.get('password')
        if all((name.strip() == 'username', password.strip() == 'password')):
            return '''
            <h1>登录成功!</h1>
            '''
        else:
            return '''
            <h1>登录失败!</h1>
            '''


@app.route('/test', methods=['GET', 'POST'])
def test():
    data = {  # 加载数据
        'title': '时雨',
        'span': 'Hello World!'
    }
    html = render_template('test.html', **data)  # 指定模板路径,解包分配
    return html  # 将渲染后的数据返回给客户端


if __name__ == '__main__':
    '''
    1. True
        · 开启Debug模式(raise Exception:有意出现的异常,开启Debug模式后使用)
    2. threaded = True
        · 开启多线程
    3. processes = 2
        · 开启2个进程(多进程)
    4. 服务对象.logger.info('')
        · 日志输出
    5. 多进程和多线程不能同时开启
    '''
    app.run('localhost', 80)