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

前言:

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_simplerun_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> redirectapp <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.mzph.cn/news/8600.shtml

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

相关文章

Ceph组件

Ceph组件 无论是想向云平台提供Ceph 对象存和 Ceph 块设备服务、部署Ceph 文件系统,或者是将 Ceph 用于其他目的,所有 Ceph 存储集群部署都从设置每个 Ceph 节点、网络开始。 一个Ceph 存储集群至少需要一个Ceph Monitor、Ceph Manager和 Ceph OSD (OBJECT STORAGE DAEMON对象存…

翻转单词顺序

剑指 Offer 58 - I. 翻转单词顺序 输入一个英文句子&#xff0c;翻转句子中单词的顺序&#xff0c;但单词内字符的顺序不变。为简单起见&#xff0c;标点符号和普通字母一样处理。例如输入字符串"I am a student. “&#xff0c;则输出"student. a am I”。 示例 1…

[虚幻引擎] UE里面监控每帧循环里面 C++ 函数的性能, 函数耗时,函数效率,函数执行时间

在使用C开发UE引擎&#xff0c;有时候需要监控函数的执行的执行效率&#xff0c;这个时候有两种方式可以使用。 1. 执行代码耗时时间 double ThisTime 0;{SCOPE_SECONDS_COUNTER(ThisTime);// ...// 一串代码// ...}UE_LOG(LogTemp, Log, TEXT("Stats::Broadcast %.2f&qu…

65英寸OLED透明屏的显示效果出色吗?

65英寸OLED透明屏是一种新型的显示技术&#xff0c;它采用有机发光二极管&#xff08;OLED&#xff09;作为显示元件&#xff0c;具有高亮度、高对比度、快速响应和广视角等优点。 与传统的液晶显示屏相比&#xff0c;OLED透明屏具有更高的透明度和更好的显示效果。 OLED透明屏…

【STM32】使用HAL库对ULN2003控制28BYJ-48步进电机

步进电机是将电脉冲信号转变为角位移或线位移&#xff0c;通过控制施加在电机线圈上的电脉冲顺序、频率和数量&#xff0c;可以控制步进电机的转向、速度和旋转角度。 配合以直线运动执行机构(螺纹丝杆)或齿轮箱装置&#xff0c;更可以实现更加复杂、精密的线性运动控制要求。…

PDF文件转换成word软件有哪些?分享两个文件格式转换软件

在日常办公中&#xff0c;我们经常使用各种办公软件&#xff0c;其中PDF和Word是最常见的两种格式。相较于Word文件&#xff0c;PDF文件具有更强的兼容性和安全性&#xff0c;因此我们通常会选择以PDF格式分享文件。然而&#xff0c;如果我们需要提取PDF文件中的部分内容&#…

计算机启动过程uefi+gpt方式

启动过程&#xff1a; 一、通电 按下开关&#xff0c;不用多说 二、uefi阶段 通电后&#xff0c;cpu第一条指令是执行uefi固件代码。 uefi固件代码固化在主板上的rom中。 &#xff08;一&#xff09;uefi介绍 UEFI&#xff0c;全称Unified Extensible Firmware Interface&am…

JVM运行时数据区——字符串常量池位置的调整

在JDK6及之前&#xff0c;使用永久代来实现方法区&#xff0c;字符串常量池(StringTable)是在永久代(方法区)中的&#xff0c;但是方法区的回收效率不高&#xff0c;在Full GC时才会回收。 在JDK7中&#xff0c;将字符串常量池转移到了堆中&#xff0c;分配在年轻代和老年代中。…

linux 使用nethogs命令查看各个进程使用网络的情况

1&#xff0c;使用nethogs命令查看各个进程使用网络的情况 #yum -y install nethogs 2&#xff0c;使用nethogs命令查看进程使用网络的情况 #nethogs

PHP8知识详解:PHP8的新特性

PHP 8是PHP编程语言的一个主要版本&#xff0c;在2020年11月26日发布。它引入了许多新特性和改进&#xff0c;包括以下一些主要特性&#xff1a; 1. JIT 编译器&#xff1a;PHP 8引入了名为Tracing JIT的即时&#xff08;Just-In-Time&#xff09;编译器。JIT可以将PHP脚本中频…

【C++ 进阶】继承

一.继承的定义格式 基类又叫父类&#xff0c;派生类又叫子类&#xff1b; 二.继承方式 继承方式分为三种&#xff1a; 1.public继承 2.protected继承 3.private继承 基类成员与继承方式的关系共有9种&#xff0c;见下表&#xff1a; 虽然说是有9种&#xff0c;但其实最常用的还…

Stable diffusion 和 Midjourney 怎么选?

通过这段时间的摸索&#xff0c;我将和你探讨&#xff0c;对普通人来说&#xff0c;Stable diffusion 和 Midjourney 怎么选&#xff1f;最重要的是&#xff0c;学好影视后期制作对 AI 绘画创作有哪些帮助&#xff1f;反过来&#xff0c;AI 绘画对影视后期又有哪些帮助&#xf…

28.JavaWeb-Elasticsearch

1.Elasticsearch概述 Elasticsearch 是一个分布式的全文检索引擎。采用Java语言开发&#xff0c;基于Apache协议的开源项目&#xff0c;具有实时搜索&#xff0c;稳定&#xff0c;可靠&#xff0c;快速的特点。 1.1 全文检索引擎 分为通用搜索引擎&#xff08;百度、谷歌&…

【深度学习】【Image Inpainting】Generative Image Inpainting with Contextual Attention

Generative Image Inpainting with Contextual Attention DeepFillv1 (CVPR’2018) 论文&#xff1a;https://arxiv.org/abs/1801.07892 论文代码&#xff1a;https://github.com/JiahuiYu/generative_inpainting 论文摘录 文章目录 效果一览摘要介绍论文贡献相关工作Image…

【Java SE】类和对象

目录 【1】面向对象的初步认识 【1.1】什么是面向对象 【1.2】面向对象与面向过程 【2】类定义和使用 【2.1】简单认识类 【2.2】类的定义格式 【2.3】练习 【2.3.1】定义一个狗类 【2.3.2】定义一个学生类 【3】类的实例化 【3.1】什么是实例化 【3.2】类和对象的…

【Java基础学习打卡18】运算符(上)

目录 前言一、运算符和表达式1.运算符2.表达式 二、算术运算符1.加法运算符2.减法运算符3.乘法运算符4.除法运算符5.取余运算符6.表达式类型自动提升 总结 前言 本文主要介绍运算符和表达式&#xff0c;及运算符中的算术运算符。在 Java 编程中&#xff0c;运算符起着非常重要…

Java 使用 Google Guava 实现接口限流

一、引入依赖 <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>30.0-jre</version> </dependency>二、自定义注解及限流拦截器 自定义注解&#xff1a;Limiter package com.haita…

安装requests模块及其他依赖库的完美解决办法

业务场景 导入requests库时报错&#xff0c;单独离线下载安装requests&#xff0c;发现仍然报错&#xff0c;问题在于requests库有其他依赖库。 WARNING: Retrying (Retry(total1, connectNone, readNone, redirectNone, statusNone)) after connection broken by NewConnect…

PgSQL-使用技巧-如何衡量网络对性能的影响

PgSQL-使用技巧-如何衡量网络对性能的影响 PG数据库和应用之间常见的部件有连接池、负载平衡组件、路由、防火墙等。我们常常不在意或者认为涉及的网络hops对整体性能产生的额外开销是理所当然的。但在很多情况下&#xff0c;它可能会导致严重的性能损失和拖累整体吞吐量。相当…

Set集合类详解(附加思维导图)

目录 一、Set集合思维导图 二、set集合类常用方法 2.1、HashSet集合常用方法 2.2、TreeSet集合的使用 三、HashSet、LinkedHashSet、TreeSet的使用场景 四、list和set集合的区别 一、Set集合思维导图 二、set集合类常用方法 2.1、HashSet集合常用方法 ①&#xff1a;add…