一. flask依赖wsgi,实现wsgi的模块:wsgiref(django),werkzeug(flask),uwsgi

1. werkzeug示例

from werkzeug.wrappers import Request, Response

@Request.application
def hello(request):
return Response('Hello World!') if __name__ == '__main__':
from werkzeug.serving import run_simple run_simple('localhost', 4000, hello)

2. wsgiref示例:

from wsgiref.simple_server import make_server

def runserver(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html')])
return [bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ] if __name__ == '__main__':
# obj = WSGIHandler()
httpd = make_server('', 8000, runserver)
httpd.serve_forever()

3. 本质的本质 -- socket

import socket

def handle_request(client):
buf = client.recv(1024)
client.send("HTTP/1.1 200 OK\r\n\r\n")
client.send("Hello, Seven") def main():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 8000))
sock.listen(5) while True:
connection, address = sock.accept()
handle_request(connection)
connection.close() if __name__ == '__main__':
main()

4. app.run()中的run方法源码

def run(self, host=None, port=None, debug=None, **options):
"""Runs the application on a local development server. Do not use ``run()`` in a production setting. It is not intended to
meet security and performance requirements for a production server.
Instead, see :ref:`deployment` for WSGI server recommendations. If the :attr:`debug` flag is set the server will automatically reload
for code changes and show a debugger in case an exception happened. If you want to run the application in debug mode, but disable the
code execution on the interactive debugger, you can pass
``use_evalex=False`` as parameter. This will keep the debugger's
traceback screen active, but disable code execution. It is not recommended to use this function for development with
automatic reloading as this is badly supported. Instead you should
be using the :command:`flask` command line script's ``run`` support. .. admonition:: Keep in Mind Flask will suppress any server error with a generic error page
unless it is in debug mode. As such to enable just the
interactive debugger without the code reloading, you have to
invoke :meth:`run` with ``debug=True`` and ``use_reloader=False``.
Setting ``use_debugger`` to ``True`` without being in debug mode
won't catch any exceptions because there won't be any to
catch. .. versionchanged:: 0.10
The default port is now picked from the ``SERVER_NAME`` variable. :param host: the hostname to listen on. Set this to ``'0.0.0.0'`` to
have the server available externally as well. Defaults to
``'127.0.0.1'``.
:param port: the port of the webserver. Defaults to ``5000`` or the
port defined in the ``SERVER_NAME`` config variable if
present.
:param debug: if given, enable or disable debug mode.
See :attr:`debug`.
:param options: the options to be forwarded to the underlying
Werkzeug server. See
:func:`werkzeug.serving.run_simple` for more
information.
"""
from werkzeug.serving import run_simple
if host is None:
host = '127.0.0.1'
if port is None:
server_name = self.config['SERVER_NAME']
if server_name and ':' in server_name:
port = int(server_name.rsplit(':', 1)[1])
else:
port = 5000
if debug is not None:
self.debug = bool(debug)
options.setdefault('use_reloader', self.debug)
options.setdefault('use_debugger', self.debug)
try:
run_simple(host, port, self, **options)
finally:
# reset the first request information if the development server
# reset normally. This makes it possible to restart the server
# without reloader and that stuff from an interactive shell.
self._got_first_request = False

基本流程



图片来源:https://blog.csdn.net/bestallen/article/details/54342120

Flask - 底层原理和基本流程的更多相关文章

  1. 操作系统底层原理与Python中socket解读

    目录 操作系统底层原理 网络通信原理 网络基础架构 局域网与交换机/网络常见术语 OSI七层协议 TCP/IP五层模型讲解 Python中Socket模块解读 TCP协议和UDP协议 操作系统底层原理 ...

  2. 拜托!面试请不要再问我Spring Cloud底层原理

    概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓 ...

  3. 深入源码分析SpringMVC底层原理(二)

    原文链接:深入源码分析SpringMVC底层原理(二) 文章目录 深入分析SpringMVC请求处理过程 1. DispatcherServlet处理请求 1.1 寻找Handler 1.2 没有找到 ...

  4. 拜托!面试请不要再问我Spring Cloud底层原理[z]

    [z]https://juejin.im/post/5be13b83f265da6116393fc7 拜托!面试请不要再问我Spring Cloud底层原理 欢迎关注微信公众号:石杉的架构笔记(id: ...

  5. MVC底层原理

    窥探ASP.Net MVC底层原理 实现跨越Session的分布式TempData 1.问题的引出 我相信大家在项目中都使用过TempData,TempData是一个字典集合,一般用于两个请求之间临时 ...

  6. Lucene底层原理和优化经验分享(1)-Lucene简介和索引原理

    Lucene底层原理和优化经验分享(1)-Lucene简介和索引原理 2017年01月04日 08:52:12 阅读数:18366 基于Lucene检索引擎我们开发了自己的全文检索系统,承担起后台PB ...

  7. 【Socket】linux黑客之网络嗅探底层原理

      1.mystery引入 1)网络嗅探属于网络攻防类的安全软件,其基于原始套接字技术开发的 2)原始套接字是一种套接字底层技术,它工作在网络层 3)谈到网络安全,刚好本学期学过这门课程,这里myst ...

  8. 【转载】Spring Cloud底层原理

    概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓 ...

  9. FPGA组成、工作原理和开发流程

    FPGA组成.工作原理和开发流程 原创 2012年01月07日 09:11:52 9402 0 4 ********************************LoongEmbedded***** ...

随机推荐

  1. [lua]紫猫lua教程-命令宝典-L1-01-01. Lua环境与IDE

    网上大把的lua教程  不过紫猫老师的教程向来都是讲的非常仔细 所以最近天气已经36+了 魔兽世界还需要冲飞行声望  懒得写单子根本没有单子,正好认认真真的看下紫猫老师的lua教程 紫猫老师的lua教 ...

  2. eclipse的版本代号

    mars为4.5版本号 代号 代号名 发布日期Eclipse 3.1 IO 木卫一,伊奥 2005Eclipse 3.2 Callisto 木卫四,卡里斯托 2006Eclipse 3.3 Europ ...

  3. 常用的vi的命令模式下的快捷命令

    知识点 在VI命令模式下:y 表示拷贝, d 表示删除,p标识粘贴 1.删除 dw 表示删除从当前光标到光标所在单词结尾的内容. d0 表示删除从当前光标到光标所在行首的内容. d$ 表示删除从当前光 ...

  4. P & R 9

    Floorplan: 要做好floorplan需要掌握哪些知识跟技能? 明确Floorplan 处理的对象:对于数字设计的 Floorplan 来说,它是一个很依赖前后步骤的一个过程,这个可以看作是后 ...

  5. 「JSOI2016」灯塔

    「JSOI2016」灯塔 传送门 我们先只计算照亮左边的灯塔的最低高度,计算右边的类同,然后只要取 \(\max\) 就好了. 那么稍微整理一下式子:\(p_i \ge h_j - h_i + \sq ...

  6. SpringCloud全家桶学习之客户端负载均衡及自定义负载均衡算法----Ribbon(三)

    一.Ribbon是什么? Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端  负载均衡的工具(这里区别于nginx的负载均衡).简单来说,Ribbon是Netf ...

  7. centos7一步一步搭建docker tomcat 及重点讲解

    系统环境:centos7.7 (VMware中) image版本:tomcat:8-jdk8-openjdk (截止2020.01.10该系列版本) 安装步骤参考文章:https://www.jian ...

  8. 顾家办公两不误,容智ibot帮你实现高效居家办公

    春节假期结束,大部分企业已陆续开始复工.经调查显示,受新型冠状病毒疫情影响,不少企业开放了员工“在家办公“模式,就此,员工被动“SOHO”,在家办公火了. 2020 在家办公靠谱吗?会不会成为未来的趋 ...

  9. Java连载65-自定义手动抛出异常、子类的异常范围、数组初探

    一.手动抛出异常1.自定义无效名字异常: (1)编译时异常,直接继承Exception (2)运行时异常,直接继承RuntimeException 举例子:注意点:throws会向上抛出异常,跑到最上 ...

  10. mysql的一些不常用语句

    今天写项目,用的ThinkPHP,写的时候有点费劲,原因嘛 无非是对框架或者mysql的一些知识的遗忘. 1.为了配合ThinkPHP中的控制器,需要修改数据库中表名,一时想不起来,幸好有百度,问题很 ...