关于我
一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android、Python、Java和Go,这个也是咱们团队的主要技术栈。
Github:https://github.com/hylinux1024
微信公众号:终身开发者(angrycode)html
Web Server Gateway Interface
它由Python
标准定义的一套Web Server
与Web Application
的接口交互规范。python
WSGI
不是一个应用、框架、模块或者库,而是规范。linux
那什么是Web Server
(Web
服务器)和什么是Web Application
(Web
应用)呢?
举例子来讲明容易理解,例如常见的Web
应用框架有Django
、Flask
等,而Web
服务器有uWSGI
、Gunicorn
等。WSGI
就是定义了这两端接口交互的规范。git
Werkzeug is a comprehensive WSGI web application library.github
Werkzeug
是一套实现WSGI
规范的函数库。咱们可使用它来建立一个Web Application
(Web
应用)。例如本文介绍的Flask
应用框架就是基于Werkzeug
来开发的。web
这里咱们使用Werkzeug
启动一个简单的服务器应用flask
from werkzeug.wrappers import Request, Response @Request.application def application(request): return Response('Hello, World!') if __name__ == '__main__': from werkzeug.serving import run_simple run_simple('localhost', 4000, application)
运行以后能够在控制台上将看到以下信息浏览器
* Running on http://localhost:4000/ (Press CTRL+C to quit)
使用浏览器打开 http://localhost:4000/ 看到如下信息,说明服务器
Hello, World!
Flask is a lightweight WSGI web application framework.微信
Flask
是一个轻量级的web
应用框架,它是跑在web
服务器中的一个应用。Flask
底层就是封装的Werkzeug
。
使用Flask
开发一个web
应用很是简单
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return f'Hello, World!' if __name__ == '__main__': app.run()
很简单吧。
接下来咱们看看Flask
应用的启动流程。
从项目地址 https://github.com/pallets/flask 中把源码clone
下来,而后切换到0.1版本的tag
。为什么要使用0.1版本呢?由于这个是做者最开始写的版本,代码量应该是最少的,并且能够很容易看到做者总体编码思路。
下面就从最简单的Demo
开始看看Flask
是如何启动的。咱们知道程序启动是执行了如下方法
if __name__ == '__main__': app.run()
而
app = Flask(__name__)
打开Flask
源码中的__init__
方法
def __init__(self, package_name): #: 是否打开debug模式 self.debug = False #: 包名或模块名 self.package_name = package_name #: 获取app所在目录 self.root_path = _get_package_path(self.package_name) #: 存储视图函数的字典,键为函数名称,值为函数对象,使用@route装饰器进行注册 self.view_functions = {} #: 存储错误处理的字典. 键为error code, 值为处理错误的函数,使用errorhandler装饰器进行注册 self.error_handlers = {} #: 处理请求前执行的函数列表,使用before_request装饰器进行注册 self.before_request_funcs = [] #: 处理请求前执行的函数列表,使用after_request装饰器进行注册 self.after_request_funcs = [] #: 模版上下文 self.template_context_processors = [_default_template_ctx_processor] #: url 映射 self.url_map = Map() #: 静态文件 if self.static_path is not None: self.url_map.add(Rule(self.static_path + '/<filename>', build_only=True, endpoint='static')) if pkg_resources is not None: target = (self.package_name, 'static') else: target = os.path.join(self.root_path, 'static') self.wsgi_app = SharedDataMiddleware(self.wsgi_app, { self.static_path: target }) #: 初始化 Jinja2 模版环境. self.jinja_env = Environment(loader=self.create_jinja_loader(), **self.jinja_options) self.jinja_env.globals.update( url_for=url_for, get_flashed_messages=get_flashed_messages )
在Flask
的构造函数中进行了各类初始化操做。
而后就是执行app.run()
方法
def run(self, host='localhost', port=5000, **options): """启动本地开发服务器. 若是debug设置为True,那么会自动检查代码是否改动,有改动则会自动执行部署 :param host: 监听的IP地址. 若是设置为 ``'0.0.0.0'``就能够进行外部访问 :param port: 端口,默认5000 :param options: 这个参数主要是对应run_simple中须要的参数 """ from werkzeug.serving import run_simple if 'debug' in options: self.debug = options.pop('debug') options.setdefault('use_reloader', self.debug) options.setdefault('use_debugger', self.debug) return run_simple(host, port, self, **options)
run
很简洁,主要是调用了werkzeug.serving
中的run_simple
方法。
再打开run_simple
的源码
def run_simple(hostname, port, application, use_reloader=False, use_debugger=False, use_evalex=True, extra_files=None, reloader_interval=1, threaded=False, processes=1, request_handler=None, static_files=None, passthrough_errors=False, ssl_context=None): # 这方法仍是比较短的,可是注释写得很详细,因为篇幅问题,就把源码中的注释省略了 if use_debugger: from werkzeug.debug import DebuggedApplication application = DebuggedApplication(application, use_evalex) if static_files: from werkzeug.wsgi import SharedDataMiddleware application = SharedDataMiddleware(application, static_files) def inner(): make_server(hostname, port, application, threaded, processes, request_handler, passthrough_errors, ssl_context).serve_forever() if os.environ.get('WERKZEUG_RUN_MAIN') != 'true': display_hostname = hostname != '*' and hostname or 'localhost' if ':' in display_hostname: display_hostname = '[%s]' % display_hostname _log('info', ' * Running on %s://%s:%d/', ssl_context is None and 'http' or 'https', display_hostname, port) if use_reloader: # Create and destroy a socket so that any exceptions are raised before # we spawn a separate Python interpreter and lose this ability. test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) test_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) test_socket.bind((hostname, port)) test_socket.close() run_with_reloader(inner, extra_files, reloader_interval) else: inner()
在rum_simple
方法中还定义一个嵌套方法inner()
,这个是方法的核心部分。
def inner(): make_server(hostname, port, application, threaded, processes, request_handler, passthrough_errors, ssl_context).serve_forever()
在inner()
方法里面,调用make_server(...).serve_forever()
启动了服务。
def make_server(host, port, app=None, threaded=False, processes=1, request_handler=None, passthrough_errors=False, ssl_context=None): """Create a new server instance that is either threaded, or forks or just processes one request after another. """ if threaded and processes > 1: raise ValueError("cannot have a multithreaded and " "multi process server.") elif threaded: return ThreadedWSGIServer(host, port, app, request_handler, passthrough_errors, ssl_context) elif processes > 1: return ForkingWSGIServer(host, port, app, processes, request_handler, passthrough_errors, ssl_context) else: return BaseWSGIServer(host, port, app, request_handler, passthrough_errors, ssl_context)
在make_server()
中会根据线程或者进程的数量建立对应的WSGI
服务器。Flask
在默认状况下是建立BaseWSGIServer
服务器。
class BaseWSGIServer(HTTPServer, object): ... class ThreadedWSGIServer(ThreadingMixIn, BaseWSGIServer): """A WSGI server that does threading.""" ... class ForkingWSGIServer(ForkingMixIn, BaseWSGIServer): """A WSGI server that does forking.""" ...
能够看出他们以前的继承关系以下
打开BaseWSGIServer
的start_server()
方法
def serve_forever(self): try: HTTPServer.serve_forever(self) except KeyboardInterrupt: pass
能够看到最终是使用HTTPServer
中的启动服务的方法。而HTTPServer
是Python
标准类库中的接口。
HTTPServer
是socketserver.TCPServer
的子类
若是要使用Python
中类库启动一个http server
,则相似代码应该是这样的
import http.server import socketserver PORT = 8000 Handler = http.server.SimpleHTTPRequestHandler with socketserver.TCPServer(("", PORT), Handler) as httpd: print("serving at port", PORT) httpd.serve_forever()
至此,整个服务的启动就到这里就启动起来了。
这个过程的调用流程为
graph TD A[Flask]-->B[app.run] B[app.run]-->C[werkzeug.run_simple] C[werkzeug.run_simple]-->D[BaseWSGIServer] D[BaseWSGIServer]-->E[HTTPServer.serve_forever] E[HTTPServer.serve_forever]-->F[TCPServer.serve_forever]
WSGI
是WEB
服务器与WEB
应用之间交互的接口规范。werkzeug
是实现了这一个规范的函数库,而Flask
框架是基于werkzeug
来实现的。
咱们从Flask.run()
方法启动服务开始,追踪了整个服务启动的流程。