flask介绍、快速使用、配置文件、路由系统

news2024/11/24 9:10:30

前言:

Flask框架和Django框架的区别:

 

  • Django框架:
    • 大而全,内置的app的很多,第三方app也很多
  • Flask框架:
    • 小而精,没有过多的内置app,只能完成web框架的基本功能,很多功能都需要借助第三方

拓展

  • python异步框架:

    • fastapi:FastAPI
    • sanic
    • tornado(目前很少人在使用)
    • django:3.x以后版本支持异步
  • 同步框架和异步框架的区别

    • 同步框架: 一个线程只会处理一个请求
    • 异步框架: 一个线程可以处理多个请求
    • 异步框架可以显著的提高并发量

一、flask介绍

1、介绍

Flask是一个基于Python开发并且依赖于jinja2模板和Werkzeug WSGI服务的一个微型框架

jinja2:

模板语法,和django的dtl非常像

Werkzeug WSGI:

符合wsgi协议的web服务器,django使用的是wsgiref

2、使用两种协议编写web

使用wsgiref编写web

<span style="color:#393a34"><span style="background-color:white"><code class="language-py"><span style="color:#0000ff">from</span> wsgiref<span style="color:#393a34">.</span>simple_server <span style="color:#0000ff">import</span> make_server


<span style="color:#0000ff">def</span> <span style="color:#393a34">mya</span><span style="color:#393a34">(</span>environ<span style="color:#393a34">,</span> start_response<span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># request就是environ包装后的对象</em></span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>environ<span style="color:#393a34">)</span>
    start_response<span style="color:#393a34">(</span><span style="color:#a31515">'200 OK'</span><span style="color:#393a34">,</span> <span style="color:#393a34">[</span><span style="color:#393a34">(</span><span style="color:#a31515">'Content-Type'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text/html'</span><span style="color:#393a34">)</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
    <span style="color:#008000"><em># 分发路由</em></span>
    <span style="color:#008000"><em># 根据用户访问的路由,打开对应的html文件,读取并返回给用户</em></span>
    <span style="color:#0000ff">if</span> environ<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'PATH_INFO'</span><span style="color:#393a34">)</span> <span style="color:#393a34">==</span> <span style="color:#a31515">'/index'</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">with</span> open<span style="color:#393a34">(</span><span style="color:#a31515">'index.html'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'rb'</span><span style="color:#393a34">)</span> <span style="color:#0000ff">as</span> f<span style="color:#393a34">:</span>
            data <span style="color:#393a34">=</span> f<span style="color:#393a34">.</span>read<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">elif</span> environ<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'PATH_INFO'</span><span style="color:#393a34">)</span> <span style="color:#393a34">==</span> <span style="color:#a31515">'/login'</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">with</span> open<span style="color:#393a34">(</span><span style="color:#a31515">'login.html'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'rb'</span><span style="color:#393a34">)</span> <span style="color:#0000ff">as</span> f<span style="color:#393a34">:</span>
            data <span style="color:#393a34">=</span> f<span style="color:#393a34">.</span>read<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
        data <span style="color:#393a34">=</span> <span style="color:#a31515">b'<h1>Hello Web!</h1>'</span>
    <span style="color:#0000ff">return</span> <span style="color:#393a34">[</span>data<span style="color:#393a34">]</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 第一个参数是服务的IP(不写默认为127.0.0.1),第二个是监听的端口,第三个是编写的web函数</em></span>
    my_server <span style="color:#393a34">=</span> make_server<span style="color:#393a34">(</span><span style="color:#a31515">'0.0.0.0'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">8008</span><span style="color:#393a34">,</span> mya<span style="color:#393a34">)</span>
    <span style="color:#008000"><em># 启动服务</em></span>
    my_server<span style="color:#393a34">.</span>serve_forever<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>

werkzeug WSGI编写服务:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># pip 安装werkzeug</em></span>
<span style="color:#008000"><em># 导入</em></span>
<span style="color:#0000ff">from</span> werkzeug<span style="color:#393a34">.</span>wrappers <span style="color:#0000ff">import</span> Request<span style="color:#393a34">,</span> Response


<span style="color:#393a34">@Request<span style="color:#393a34">.</span>application</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">my_server</span><span style="color:#393a34">(</span>request<span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>request<span style="color:#393a34">)</span>
    <span style="color:#0000ff">return</span> Response<span style="color:#393a34">(</span><span style="color:#a31515">'Hello Web!'</span><span style="color:#393a34">)</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 导入启动服务的模块</em></span>
    <span style="color:#0000ff">from</span> werkzeug<span style="color:#393a34">.</span>serving <span style="color:#0000ff">import</span> run_simple

    run_simple<span style="color:#393a34">(</span><span style="color:#a31515">'127.0.0.1'</span><span style="color:#393a34">,</span> <span style="color:#36acaa">4000</span><span style="color:#393a34">,</span> my_server<span style="color:#393a34">)</span>
</code></span></span>

二、flask快速使用

安装:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 安装flask会一并安装其依赖:jinja2、Werkzeug、MarkupSafe</em></span>
	pip install flask 

<span style="color:#008000"><em># 版本问题:</em></span>
    <span style="color:#393a34">-</span><span style="color:#36acaa">1</span><span style="color:#393a34">.</span>x 没有本质区别
    <span style="color:#393a34">-</span><span style="color:#36acaa">2</span><span style="color:#393a34">.</span>x 没有本质区别,源码上动了,用起来一样
</code></span></span>

1、快速使用:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 导入模块</em></span>
<span style="color:#0000ff">from</span> flask <span style="color:#0000ff">import</span> Flask

<span style="color:#008000"><em># 实例化对象,参数内是服务的名字,填入任意都可以</em></span>

app <span style="color:#393a34">=</span> Flask<span style="color:#393a34">(</span>__name__<span style="color:#393a34">)</span>


<span style="color:#008000"><em># 编写函数、注册路由(装饰器方法注册)</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#0000ff">return</span> <span style="color:#a31515">'hello web!'</span>


<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/home'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">home</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#0000ff">return</span> <span style="color:#a31515">'hello home!'</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># app.run('127.0.0.1', 5000)</em></span>
    <span style="color:#008000"><em># 默认监听本地127.0.0.1的5000端口</em></span>
    app<span style="color:#393a34">.</span>run<span style="color:#393a34">(</span><span style="color:#393a34">)</span>
</code></span></span>

2、使用flask编写登录小案例

2.1 login.html

<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>

<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>form</span> <span style="color:#ff0000">method</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>post<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>用户名:<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>text<span style="color:#393a34">"</span></span> <span style="color:#ff0000">name</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>username<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>密码:<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>password<span style="color:#393a34">"</span></span> <span style="color:#ff0000">name</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>password<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>input</span> <span style="color:#ff0000">type</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>submit<span style="color:#393a34">"</span></span> <span style="color:#ff0000">value</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>登录<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span> {{error}}
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>form</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>

2.2 home.html

<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>h1</span><span style="color:#393a34">></span></span>用户列表<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>h1</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>table</span><span style="color:#393a34">></span></span>
    {% for k,v in user_dict.items() %}
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>tr</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{k}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v.name}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v['name']}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span>{{v.get('name')}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
        <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>td</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>a</span> <span style="color:#ff0000">href</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>/detail/{{k}}<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>查看详细<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>a</span><span style="color:#393a34">></span></span><span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>td</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>tr</span><span style="color:#393a34">></span></span>
    {% endfor %}
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>table</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>

2.3 detail.html

<span style="color:#393a34"><span style="background-color:white"><code class="language-html"><span style="color:#008000"><em><span style="color:#393a34"><!</span>DOCTYPE html<span style="color:#393a34">></span></em></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>html</span> <span style="color:#ff0000">lang</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>en<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>head</span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>meta</span> <span style="color:#ff0000">charset</span><span style="color:#0000ff"><span style="color:#393a34">=</span><span style="color:#393a34">"</span>UTF-8<span style="color:#393a34">"</span></span><span style="color:#393a34">></span></span>
    <span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>title</span><span style="color:#393a34">></span></span>Title<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>title</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>head</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>名字是:{{user.name}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>年龄是:{{user['age']}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>性别是:{{user.get('gender')}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"><</span>p</span><span style="color:#393a34">></span></span>{{user.text}}<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>p</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>body</span><span style="color:#393a34">></span></span>
<span style="color:#800000"><span style="color:#800000"><span style="color:#393a34"></</span>html</span><span style="color:#393a34">></span></span>
</code></span></span>

2.4 py文件

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#0000ff">from</span> flask <span style="color:#0000ff">import</span> Flask<span style="color:#393a34">,</span> request<span style="color:#393a34">,</span> render_template<span style="color:#393a34">,</span> session<span style="color:#393a34">,</span> redirect

app <span style="color:#393a34">=</span> Flask<span style="color:#393a34">(</span>__name__<span style="color:#393a34">)</span>

<span style="color:#008000"><em># 使用session需要指定key</em></span>
app<span style="color:#393a34">.</span>secret_key <span style="color:#393a34">=</span> <span style="color:#a31515">'abc123'</span>

USERS <span style="color:#393a34">=</span> <span style="color:#393a34">{</span>
    <span style="color:#36acaa">1</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'张三'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">18</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'男'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"道路千万条"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span>
    <span style="color:#36acaa">2</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'李四'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">28</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'男'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"安全第一条"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span>
    <span style="color:#36acaa">3</span><span style="color:#393a34">:</span> <span style="color:#393a34">{</span><span style="color:#a31515">'name'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'王五'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'age'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">18</span><span style="color:#393a34">,</span> <span style="color:#a31515">'gender'</span><span style="color:#393a34">:</span> <span style="color:#a31515">'女'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'text'</span><span style="color:#393a34">:</span> <span style="color:#a31515">"行车不规范"</span><span style="color:#393a34">}</span><span style="color:#393a34">,</span>
<span style="color:#393a34">}</span>


<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">,</span> methods<span style="color:#393a34">=</span><span style="color:#393a34">[</span><span style="color:#a31515">'GET'</span><span style="color:#393a34">,</span> <span style="color:#a31515">'POST'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 判断路由的方式</em></span>
    <span style="color:#0000ff">if</span> request<span style="color:#393a34">.</span>method <span style="color:#393a34">==</span> <span style="color:#a31515">'GET'</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 返回登陆页面给用户</em></span>
        <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Login.html'</span><span style="color:#393a34">)</span>
    <span style="color:#008000"><em># post请求判断用户名密码</em></span>
    <span style="color:#0000ff">if</span> request<span style="color:#393a34">.</span>method <span style="color:#393a34">==</span> <span style="color:#a31515">'POST'</span><span style="color:#393a34">:</span>
        username <span style="color:#393a34">=</span> request<span style="color:#393a34">.</span>form<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'username'</span><span style="color:#393a34">)</span>
        password <span style="color:#393a34">=</span> request<span style="color:#393a34">.</span>form<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'password'</span><span style="color:#393a34">)</span>
        <span style="color:#008000"><em># 校验用户名或密码</em></span>
        <span style="color:#0000ff">if</span> username <span style="color:#393a34">==</span> <span style="color:#a31515">'kangkang'</span> <span style="color:#0000ff">and</span> password <span style="color:#393a34">==</span> <span style="color:#a31515">'123'</span><span style="color:#393a34">:</span>
            <span style="color:#008000"><em># 校验成功,保存session(导入、全局使用)</em></span>
            session<span style="color:#393a34">[</span><span style="color:#a31515">'name'</span><span style="color:#393a34">]</span> <span style="color:#393a34">=</span> username
            <span style="color:#008000"><em># 重定向到home页面(导入redirect)</em></span>
            <span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
        <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
            <span style="color:#008000"><em># 用户名或密码错误</em></span>
            <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Login.html'</span><span style="color:#393a34">,</span> error<span style="color:#393a34">=</span><span style="color:#a31515">'用户名或密码错误'</span><span style="color:#393a34">)</span>


<span style="color:#008000"><em># 编写首页</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">home</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 先校验用户是否登录</em></span>
    <span style="color:#0000ff">if</span> session<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 校验登录通过,展示首页</em></span>
        <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Home.html'</span><span style="color:#393a34">,</span> user_dict<span style="color:#393a34">=</span>USERS<span style="color:#393a34">)</span>
    <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 没有登陆跳转到登陆页面</em></span>
        <span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span>


<span style="color:#008000"><em># 编写用户详情页</em></span>
<span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/detail/<int:pk>'</span><span style="color:#393a34">)</span>
<span style="color:#0000ff">def</span> <span style="color:#393a34">detail</span><span style="color:#393a34">(</span>pk<span style="color:#393a34">)</span><span style="color:#393a34">:</span>
    <span style="color:#008000"><em># 先校验用户是否登录</em></span>
    <span style="color:#0000ff">if</span> session<span style="color:#393a34">.</span>get<span style="color:#393a34">(</span><span style="color:#a31515">'name'</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 校验登录通过,展示详情页面</em></span>
        user_detail <span style="color:#393a34">=</span> USERS<span style="color:#393a34">[</span>pk<span style="color:#393a34">]</span>
        <span style="color:#0000ff">return</span> render_template<span style="color:#393a34">(</span><span style="color:#a31515">'Detail.html'</span><span style="color:#393a34">,</span> user<span style="color:#393a34">=</span>user_detail<span style="color:#393a34">)</span>
    <span style="color:#0000ff">else</span><span style="color:#393a34">:</span>
        <span style="color:#008000"><em># 没有登陆跳转到登陆页面</em></span>
        <span style="color:#0000ff">return</span> redirect<span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span>


<span style="color:#0000ff">if</span> __name__ <span style="color:#393a34">==</span> <span style="color:#a31515">'__main__'</span><span style="color:#393a34">:</span>
    app<span style="color:#393a34">.</span>run<span style="color:#393a34">(</span><span style="color:#393a34">)</span>

</code></span></span>

三、flask配置文件

1、配置文件的几种方式

flask不同于django可以在settings文件编写配置,flask配置文件的方式有多种,相较于django更加灵活

方式一:直接编写

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 在编写app的我呢见中直接编写配置(用于测试)</em></span>
    app<span style="color:#393a34">.</span>debug<span style="color:#393a34">=</span><span style="color:#36acaa">True</span>  
    <span style="color:#008000"><em># 调试模式,提示信息更详细,修改代码不需要重启,自动重启</em></span>
    
    app<span style="color:#393a34">.</span>secret_key<span style="color:#393a34">=</span><span style="color:#a31515">'dasdfasdfasd'</span>  
    <span style="color:#008000"><em># 秘钥,只能 放debug和secret_key</em></span>
</code></span></span>

方式二:使用app.config

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 直接使用flask实例化的对象点出config的方式添加</em></span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">[</span><span style="color:#a31515">'DEBUG'</span><span style="color:#393a34">]</span><span style="color:#393a34">=</span><span style="color:#36acaa">True</span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">[</span><span style="color:#a31515">'SECRET_KEY'</span><span style="color:#393a34">]</span><span style="color:#393a34">=</span><span style="color:#a31515">'sdfasdfasd'</span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span>
</code></span></span>

方式三:使用py文件,然后载入

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 将配置编写在py文件中,然后使用方法导入(不常用)</em></span>

    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_pyfile<span style="color:#393a34">(</span><span style="color:#a31515">"settings.py"</span><span style="color:#393a34">)</span>  <span style="color:#008000"><em># 变量必须大写</em></span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span>
</code></span></span>

方式四:使用类导入

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 同样是创建py文件,区别是写在类中,可以上线时候可以指定使用哪套</em></span>

    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_object<span style="color:#393a34">(</span><span style="color:#a31515">'settings.DevelopmentConfig'</span><span style="color:#393a34">)</span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_object<span style="color:#393a34">(</span><span style="color:#a31515">'settings.ProductionConfig'</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">print</span><span style="color:#393a34">(</span>app<span style="color:#393a34">.</span>config<span style="color:#393a34">)</span> 
</code></span></span>

方式五:其他方式

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 1、通过环境变量导入</em></span>
	 app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_envvar<span style="color:#393a34">(</span><span style="color:#a31515">"环境变量名称"</span><span style="color:#393a34">)</span>
    
<span style="color:#008000"><em># 2、通过json文件载入</em></span>
    app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_json<span style="color:#393a34">(</span><span style="color:#a31515">"json文件名称"</span><span style="color:#393a34">)</span>
    <span style="color:#008000"><em># JSON文件名称,必须是json格式,因为内部会执行json.loads</em></span>
    
<span style="color:#008000"><em># 3、字典格式、配置中心</em></span>
	app<span style="color:#393a34">.</span>config<span style="color:#393a34">.</span>from_mapping<span style="color:#393a34">(</span><span style="color:#393a34">{</span><span style="color:#a31515">'DEBUG'</span><span style="color:#393a34">:</span> <span style="color:#36acaa">True</span><span style="color:#393a34">}</span><span style="color:#393a34">)</span>
</code></span></span>

2、常用的配置字段

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#393a34">-</span>DEBUG  <span style="color:#008000"><em># debug模式</em></span>
<span style="color:#393a34">-</span>SECRET_KEY  <span style="color:#008000"><em># session的key值 (密钥)</em></span>
<span style="color:#393a34">-</span>SESSION_COOKIE_NAME  <span style="color:#008000"><em># 用户浏览器上cokie会变成设置的名字</em></span>
<span style="color:#393a34">-</span>PERMANENT_SESSION_LIFETIME  <span style="color:#008000"><em># session过期时间</em></span>

<span style="color:#008000"><em># 内置的配置字段,其他可以写自己的,比如 redis的连接地址,mysql的连接地址</em></span>
</code></span></span>

四、路由系统

1、路由的本质

在django中,路由写在urls.py文件下的path列表中

flask是基于装饰器的,大部分都是使用装饰器来做,少量的可以抽取到urls.py中

路由装饰器源码分析:

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># 咱们这样写</em></span>
    <span style="color:#393a34">@app<span style="color:#393a34">.</span>route</span><span style="color:#393a34">(</span><span style="color:#a31515">'/login'</span><span style="color:#393a34">)</span>
    <span style="color:#0000ff">def</span> <span style="color:#393a34">index</span><span style="color:#393a34">(</span><span style="color:#393a34">)</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">pass</span>
    
    <span style="color:#008000"><em>#本质是---》index=app.route('/login')(index)</em></span>
    
    <span style="color:#008000"><em># app.route('/login')的执行结果 decorator 函数</em></span>
    	<span style="color:#393a34">-</span>rule是路径
        <span style="color:#393a34">-</span>其他参数都给了options
    <span style="color:#008000"><em># 然后 decorator(index)--->在执行</em></span>
    		<span style="color:#008000"><em># f是index</em></span>
    		endpoint <span style="color:#393a34">=</span> options<span style="color:#393a34">.</span>pop<span style="color:#393a34">(</span><span style="color:#a31515">"endpoint"</span><span style="color:#393a34">,</span> <span style="color:#36acaa">None</span><span style="color:#393a34">)</span> <span style="color:#008000"><em># 目前没有endpoint,是None</em></span>
            <span style="color:#008000"><em># 核心,本质--》self就是实例化得到的app对象,flask对象</em></span>
            <span style="color:#008000"><em># app对象中有个方法add_url_rule,这是在添加路由</em></span>
            <span style="color:#008000"><em># 不使用装饰器,自己注册路由</em></span>
            self<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span>rule<span style="color:#393a34">,</span> endpoint<span style="color:#393a34">,</span> f<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">)</span>
            <span style="color:#0000ff">return</span> f
        
        
    <span style="color:#0000ff">def</span> <span style="color:#393a34">route</span><span style="color:#393a34">(</span>self<span style="color:#393a34">,</span> rule<span style="color:#393a34">:</span> str<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">:</span> t<span style="color:#393a34">.</span>Any<span style="color:#393a34">)</span> <span style="color:#393a34">-</span><span style="color:#393a34">></span> t<span style="color:#393a34">.</span>Callable<span style="color:#393a34">[</span><span style="color:#393a34">[</span>T_route<span style="color:#393a34">]</span><span style="color:#393a34">,</span> T_route<span style="color:#393a34">]</span><span style="color:#393a34">:</span>
        <span style="color:#0000ff">def</span> <span style="color:#393a34">decorator</span><span style="color:#393a34">(</span>f<span style="color:#393a34">:</span> T_route<span style="color:#393a34">)</span> <span style="color:#393a34">-</span><span style="color:#393a34">></span> T_route<span style="color:#393a34">:</span>
            endpoint <span style="color:#393a34">=</span> options<span style="color:#393a34">.</span>pop<span style="color:#393a34">(</span><span style="color:#a31515">"endpoint"</span><span style="color:#393a34">,</span> <span style="color:#36acaa">None</span><span style="color:#393a34">)</span>
            self<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span>rule<span style="color:#393a34">,</span> endpoint<span style="color:#393a34">,</span> f<span style="color:#393a34">,</span> <span style="color:#393a34">**</span>options<span style="color:#393a34">)</span>
            <span style="color:#0000ff">return</span> f

        <span style="color:#0000ff">return</span> decorator
    
    
<span style="color:#008000"><em># 可以不使用装饰器的方式,注册路由</em></span>
	app<span style="color:#393a34">.</span>add_url_rule<span style="color:#393a34">(</span><span style="color:#a31515">'/'</span><span style="color:#393a34">,</span> endpoint<span style="color:#393a34">=</span><span style="color:#36acaa">None</span><span style="color:#393a34">,</span> view_func<span style="color:#393a34">=</span>home<span style="color:#393a34">,</span> methods<span style="color:#393a34">=</span><span style="color:#393a34">[</span><span style="color:#a31515">'GET'</span><span style="color:#393a34">]</span><span style="color:#393a34">)</span>
    
    
<span style="color:#008000"><em># flask路由的本质是app对象的add_url_rule完成路由的注册</em></span>
</code></span></span>

2、add_url_rule参数

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"><span style="color:#008000"><em># rule             URL规则</em></span>
<span style="color:#008000"><em># view_func        视图函数名称</em></span>
<span style="color:#008000"><em># defaults = None  默认值, 当URL中无参数,函数需要参数时,使用defaults = {'k': 'v'}为函数提供参数</em></span>
<span style="color:#008000"><em># endpoint = None, 路径的别名,名称,用于反向解析URL,即: url_for('名称')</em></span>
<span style="color:#008000"><em># methods = None, 允许的请求方式,如:["GET", "POST"]</em></span>


<span style="color:#008000"><em>#对URL最后的 / 符号是否严格要求</em></span>
strict_slashes <span style="color:#393a34">=</span> <span style="color:#36acaa">None</span>
    <span style="color:#a31515">'''
        @app.route('/index', strict_slashes=False)
        #访问http://www.xx.com/index/ 或http://www.xx.com/index均可
        @app.route('/index', strict_slashes=True)
        #仅访问http://www.xx.com/index
    '''</span>

<span style="color:#008000"><em>#重定向到指定地址</em></span>
redirect_to <span style="color:#393a34">=</span> <span style="color:#36acaa">None</span><span style="color:#393a34">,</span> 
    <span style="color:#a31515">'''
        @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
    '''</span>
    
    
 <span style="color:#008000"><em># 需要记住的</em></span>
    <span style="color:#008000"><em># rule  </em></span>
    <span style="color:#008000"><em># view_func   </em></span>
    <span style="color:#008000"><em># defaults</em></span>
    <span style="color:#008000"><em># endpoint</em></span>
    <span style="color:#008000"><em># methods</em></span>
</code></span></span>

3、转换器

<span style="color:#393a34"><span style="background-color:white"><code class="language-python"> <span style="color:#a31515">'default'</span><span style="color:#393a34">:</span>          UnicodeConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'string'</span><span style="color:#393a34">:</span>           UnicodeConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'any'</span><span style="color:#393a34">:</span>              AnyConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'path'</span><span style="color:#393a34">:</span>             PathConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'int'</span><span style="color:#393a34">:</span>              IntegerConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'float'</span><span style="color:#393a34">:</span>            FloatConverter<span style="color:#393a34">,</span>
 <span style="color:#a31515">'uuid'</span><span style="color:#393a34">:</span>             UUIDConverter<span style="color:#393a34">,</span>
    
 <span style="color:#008000"><em># 了解:让路由支持正则(忽略掉)</em></span></code></span></span>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/783137.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

护城河理论

护城河理论 护城河理论|来自股神巴菲特&#xff0c;是指投资的企业在某一方面的核心竞争力。 模型介绍 在2000年的伯克希尔哈撒韦的年会上&#xff0c;巴菲特说&#xff1a;让我们来把护城河作为一个伟大企业的首要标准&#xff0c;保持它的宽度&#xff0c;保持它不被跨越。我…

【字符流】案例:集合到文件

案例&#xff1a;集合到文件 1.需求&#xff1a; 把ArrayList集合中的字符串数据写入到文本文件。要求&#xff1a;每一个字符串元素作为文件中的一行数据 2.思路 创建ArrayList集合往集合中存储字符串元素创建字符缓冲输出流对象遍历集合&#xff0c;得到每一个字符串数据调…

『Dubbo SPI源码分析』依赖注入机制分析

Dubbo Wrapper 依赖注入机制分析 基于 2.7.0 版本上一章&#xff1a;『Dubbo SPI源码分析』Wrapper 机制分析 创建测试 demo 首先创建一个接口&#xff0c;举例为 Car package com.luban.dubbo_spi.api;SPI public interface Car {public void getColor();public void getC…

数据结构(王道)——数据结构之 由遍历序列构造二叉树

结论&#xff1a;给出遍历序列当中的一种&#xff0c;不能唯一确定一颗二叉树。

[回馈]ASP.NET Core MVC开发实战之商城系统(一)

经过一段时间的准备&#xff0c;新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始&#xff0c;今天着重讲解布局设计&#xff0c;环境搭建&#xff0c;系统配置&#xff0c;及首页商品类型&#xff0c;banner条&#xff0c;友情链接等功能的开发。 首页布局设计 首页是…

Android使用Shape画格子图和圆形

觉得画来玩玩&#xff0c;比较有趣&#xff0c;记录一下。 1格子。 <?xml version"1.0" encoding"utf-8"?> <layer-list xmlns:android"http://schemas.android.com/apk/res/android"><item ><shape><solid andro…

性能测试-Jmeter之Linux下压力测试

我们在做测试的时候&#xff0c;有时候要运行很久&#xff0c;公司用的测试服务器一般都是linux&#xff0c;就可以运行在linux下面&#xff0c;linux下面不能像windows一样有图形化界面&#xff0c;那怎么运行脚本呢&#xff0c;就先在windows上把脚本做好&#xff0c;然后在l…

解决ORACLE PLSQL查询速度慢问题

在表内已建有索引情况下&#xff0c;查询速度有时快&#xff0c;有时慢的问题。 数据库&#xff1a;Oracle&#xff0c; 工具&#xff1a;PlsqlDev 不走索引的原因通常有以下几种&#xff1a; 1.索引失效或丢失&#xff1a;当数据库中的索引被减少、删除或者失效时&#xff0…

浅析JAVA虚拟机结构与机制

本文旨在给所有希望了解 可以看出&#xff0c;JVM主要由类加载器子系统、运行时数据区&#xff08;内存空间&#xff09;、执行引擎以及与本地方法接口等组成。其中运行时数据区又由方法区、堆、Java栈、PC寄存器、本地方法栈组成。 从上图中还可以看出&#xff0c;在内存空间…

Flask 创建文件目录,删除文件目录

项目结构 app.py from flask import Flask, render_template, request, redirect, url_for import osapp Flask(__name__) BASE_DIR os.path.abspath(os.path.dirname(__file__)) FILE_DIR os.path.join(BASE_DIR, testfile)app.route(/, methods[GET, POST]) def index():…

心海舟楫、三一重工面试(部分)

心海舟楫 一道算法题&#xff1a; 我开始给出的是暴力解法&#xff0c;时间复杂度O(n^2)。 在面试官的提示下&#xff0c;实现了时间复杂度为O(n)的解法。 三一重工 没啥特别的

【VTK】VTK 让小球动起来,在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK

知识不是单独的&#xff0c;一定是成体系的。更多我的个人总结和相关经验可查阅这个专栏&#xff1a;Visual Studio。 文章目录 版本环境A.uiA.hA.cppRef. 本文主要目的是在 Qt 界面中&#xff0c;显示出来使用 VTK 构建的小球&#xff0c;并让小球能够动起来。同时为了方便对比…

第2章 SparkSQL 核心编程

第2章 SparkSQL 核心编程 2.1 新的起点2.2 DataFrame2.2.1 创建 DataFrame2.2.2 SQL 语法2.2.3 DSL 语法2.2.4 RDD 转换为 DataFrame2.2.5 DataFrame 转换为 RDD 2.3 DataSet2.3.1 创建 DataSet2.3.2 RDD 转换为 DataSet2.3.3 DataSet 转换为 RDD 2.4 DataFrame 和 DataSet 转…

学习记录681@Gitlab升级实战

前言 我的Linux目前是centos8&#xff0c;目前使用的gitlab是从https://mirrors.tuna.tsinghua.edu.cn/ 下载下来的gitlab-ce-12.10.1-ce.0.el8.x86_64.rpm&#xff0c;然后安装的。 这里需要注意如果是centos8需要下载el8的gitlab&#xff0c;如果是centos7需要下载el7的git…

golang - 下载大文件,实时返回前端下载进度,实现下载进度条

示例&#xff1a; package mainimport ("fmt""io""net/http""os""path"//"github.com/kataras/iris""github.com/kataras/iris/v12""time" )func doSomething() {time.Sleep(time.Second * …

大数据学习04-Hbase分布式集群部署

系统环境&#xff1a;centos7 软件版本&#xff1a;jdk1.8、zookeeper3.4.8、hadoop2.8.5 一、下载 HBASE官网 cd /home/toolswget https://archive.apache.org/dist/hbase/2.2.4/hbase-2.2.4-bin.tar.gz二、解压 tar -zxvf hbase-2.2.4-bin.tar.gz -C /home/local/移动目…

【弹力设计篇】聊聊降级设计

我们知道在分布式系统中&#xff0c;故障是不可避免的&#xff0c;所以我们需要设计一个高可用的系统&#xff0c;对于接口层面除了幂等&重试机制&#xff0c;还需要保证接口高可用&#xff0c;因此 限流&排队&降级&熔断也需要考虑。本篇主要介绍下接口故障下降…

Qt 之 自定义json配置文件类,QJsonDocument应用

目录 一、前言 二、头文件代码 三、源文件代码 四、使用示例 五、使用效果 一、前言 Qt的配置类QSettings主要是键值结构的配置&#xff0c;若需要的配置项为树形结构&#xff0c;例如配置学校\学院\班级\学生这样&#xff0c;使用键值结构已经不满足我们的需求了&#xf…

【计算机视觉 | 图像分割】arxiv 计算机视觉关于图像分割的学术速递(7 月 21 日论文合集)

文章目录 一、分割|语义相关(14篇)1.1 CNOS: A Strong Baseline for CAD-based Novel Object Segmentation1.2 Spinal nerve segmentation method and dataset construction in endoscopic surgical scenarios1.3 WeakPolyp: You Only Look Bounding Box for Polyp Segmentatio…

【unity】模型裁剪shader(建筑生长动画)

【unity】模型裁剪shader&#xff08;建筑生长动画&#xff09; 思路 使用的核心方法是clip,当传入正值时渲染&#xff0c;传入负值时不渲染。定义一个裁剪向量&#xff0c;使用裁剪向量和模型点点乘&#xff0c;如果模型点和裁剪向量是同一个方向&#xff0c;点乘为正&#…