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

1. werkzeug示例

  1. from werkzeug.wrappers import Request, Response
  2. @Request.application
  3. def hello(request):
  4. return Response('Hello World!')
  5. if __name__ == '__main__':
  6. from werkzeug.serving import run_simple
  7. run_simple('localhost', 4000, hello)

2. wsgiref示例:

  1. from wsgiref.simple_server import make_server
  2. def runserver(environ, start_response):
  3. start_response('200 OK', [('Content-Type', 'text/html')])
  4. return [bytes('<h1>Hello, web!</h1>', encoding='utf-8'), ]
  5. if __name__ == '__main__':
  6. # obj = WSGIHandler()
  7. httpd = make_server('', 8000, runserver)
  8. httpd.serve_forever()

3. 本质的本质 -- socket

  1. import socket
  2. def handle_request(client):
  3. buf = client.recv(1024)
  4. client.send("HTTP/1.1 200 OK\r\n\r\n")
  5. client.send("Hello, Seven")
  6. def main():
  7. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8. sock.bind(('localhost', 8000))
  9. sock.listen(5)
  10. while True:
  11. connection, address = sock.accept()
  12. handle_request(connection)
  13. connection.close()
  14. if __name__ == '__main__':
  15. main()

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

  1. def run(self, host=None, port=None, debug=None, **options):
  2. """Runs the application on a local development server.
  3. Do not use ``run()`` in a production setting. It is not intended to
  4. meet security and performance requirements for a production server.
  5. Instead, see :ref:`deployment` for WSGI server recommendations.
  6. If the :attr:`debug` flag is set the server will automatically reload
  7. for code changes and show a debugger in case an exception happened.
  8. If you want to run the application in debug mode, but disable the
  9. code execution on the interactive debugger, you can pass
  10. ``use_evalex=False`` as parameter. This will keep the debugger's
  11. traceback screen active, but disable code execution.
  12. It is not recommended to use this function for development with
  13. automatic reloading as this is badly supported. Instead you should
  14. be using the :command:`flask` command line script's ``run`` support.
  15. .. admonition:: Keep in Mind
  16. Flask will suppress any server error with a generic error page
  17. unless it is in debug mode. As such to enable just the
  18. interactive debugger without the code reloading, you have to
  19. invoke :meth:`run` with ``debug=True`` and ``use_reloader=False``.
  20. Setting ``use_debugger`` to ``True`` without being in debug mode
  21. won't catch any exceptions because there won't be any to
  22. catch.
  23. .. versionchanged:: 0.10
  24. The default port is now picked from the ``SERVER_NAME`` variable.
  25. :param host: the hostname to listen on. Set this to ``'0.0.0.0'`` to
  26. have the server available externally as well. Defaults to
  27. ``'127.0.0.1'``.
  28. :param port: the port of the webserver. Defaults to ``5000`` or the
  29. port defined in the ``SERVER_NAME`` config variable if
  30. present.
  31. :param debug: if given, enable or disable debug mode.
  32. See :attr:`debug`.
  33. :param options: the options to be forwarded to the underlying
  34. Werkzeug server. See
  35. :func:`werkzeug.serving.run_simple` for more
  36. information.
  37. """
  38. from werkzeug.serving import run_simple
  39. if host is None:
  40. host = '127.0.0.1'
  41. if port is None:
  42. server_name = self.config['SERVER_NAME']
  43. if server_name and ':' in server_name:
  44. port = int(server_name.rsplit(':', 1)[1])
  45. else:
  46. port = 5000
  47. if debug is not None:
  48. self.debug = bool(debug)
  49. options.setdefault('use_reloader', self.debug)
  50. options.setdefault('use_debugger', self.debug)
  51. try:
  52. run_simple(host, port, self, **options)
  53. finally:
  54. # reset the first request information if the development server
  55. # reset normally. This makes it possible to restart the server
  56. # without reloader and that stuff from an interactive shell.
  57. 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. 201771010135-杨蓉庆 实验一 软件工程准备—用Markdown写构建之法

    项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh   博客园  https://www.cnblogs.com/nwnu-daizh/p/12369881.h ...

  2. 2019 徐州网络赛 center

    题意:n个点,求最小加上几个点让所有点关于一个点(不需要是点集里面的点)中心对称 题解:双重循环枚举,把中点记录一下,结果是n-最大的中点 #include <bits/stdc++.h> ...

  3. 【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)

    题意: 输入两个正整数N和M(N<=500,M<=N^2),分别代表点数和边数.接着输入M行每行包括一条边的两个结点(0~N-1),这条路的长度和通过这条路所需要的时间.接着输入两个整数表 ...

  4. ALSA driver--Asoc

    https://blog.csdn.net/zyuanyun/article/details/59170418 ALSA Asoc框架如下图: Asoc分为machine,platform,codec ...

  5. python闯关之路二(模块的应用)

    1.有如下字符串:n = "路飞学城"(编程题) - 将字符串转换成utf-8的字符编码的字节,再将转换的字节重新转换为utf-8的字符编码的字符串 - 将字符串转换成gbk的字符 ...

  6. jmeter 进行websocket数据帧过大导致code 1009

    Response message: Received: Close frame with status code 1009 and close reason 'No async message sup ...

  7. 极客从CPU选择开始-CPU详解

    先来看看CPU天梯图(来自(快科技CPU性能天梯图)[https://www.mydrivers.com/zhuanti/tianti/cpu/index.html]) Intel VS AMD (P ...

  8. Mysql与PostgreSql数据库学习笔记

    mysql 从最基础的数据引擎,到进程结构,都不能支持数据版本.导致其职能阻塞“并发”,不支持最基本的事务,innodb达不到基本事务要求,任何写数据,都导致整个表锁住.充其量只能算是一个玩具,或者说 ...

  9. jquery 根据值 设置radio选中

    $("[name='selector'][value='value']").prop("checked", "checked");

  10. Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException

    实体类缺少无参构造方法,序列化对象需要无参构造方法 @NoArgsConstructor