Flask源码解读(一)
Flask是一个使用 Python 编写的轻量级 Web 应用框架。Flask 本身只是 Werkezug 和 Jinja2 的之间的桥梁,前者实现一个合适的 WSGI 应用,后者处理模板。 当然, Flask 也绑定了一些通用的标准库包,比如 logging 。 除此之外其它所有一切都交给扩展来实现。我将追踪一个简单FlaskApp的运行,看看request和response是怎么实现,以下是一个简单的flask app代码,可用浏览器访问
#!/usr/bin/env python # encoding: utf-8 from flask import Flask app = Flask(__name__) @app.route('/') def index(): import pdb;pdb.set_trace() return '<h1>Hello world!</h1>' @app.route('/user/<name>') def user(name): import pdb;pdb.set_trace() return '<h1>Hello,%s!</h1>' % name if __name__ == '__main__': app.run()
通过pdb捕捉栈的调用情况,可以得到
/home/steinliber/flask-source-code/route/a.py(18)<module>()
-> app.run()
/home/steinliber/flask-source-code/env/local/lib/python2.7/site-packages/flask/app.py(772)run()
-> run_simple(host, port, self, **options)
/home/steinliber/flask-source-code/env/local/lib/python2.7/site-packages/werkzeug/serving.py(692)run_simple()
-> inner()
/home/steinliber/flask-source-code/env/local/lib/python2.7/site-packages/werkzeug/serving.py(657)inner()
-> srv.serve_forever()
/home/steinliber/flask-source-code/env/local/lib/python2.7/site-packages/werkzeug/serving.py(497)serve_forever()
-> HTTPServer.serve_forever(self)
这是捕捉到的启动flask服务器调用的函数,从中可以看到因为app是Flask的实例,则调用app.run()会调用Flask类中的run(),而run()方法只是简单的为host和port设置了默认值就调用了werkzeug的run_simple()
在werkzeug中的run_simple()函数
def run_simple(hostname, port, application, use_reloader=False, use_debugger=False, use_evalex=True, extra_files=None, reloader_interval=1, reloader_type='auto', 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 log_startup(sock): display_hostname = hostname not in ('', '*') and hostname or 'localhost' if ':' in display_hostname: display_hostname = '[%s]' % display_hostname quit_msg = '(Press CTRL+C to quit)' port = sock.getsockname()[1] _log('info', ' * Running on %s://%s:%d/ %s', ssl_context is None and 'http' or 'https', display_hostname, port, quit_msg) def inner(): try: fd = int(os.environ['WERKZEUG_SERVER_FD']) except (LookupError, ValueError): fd = None srv = make_server(hostname, port, application, threaded, processes, request_handler, passthrough_errors, ssl_context, fd=fd) if fd is None: log_startup(srv.socket) srv.serve_forever()
if use_reloader:
# If we're not running already in the subprocess that is the
# reloader we want to open up a socket early to make sure the
# port is actually available.
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
if port == 0 and not can_open_by_fd:
raise ValueError('Cannot bind to a random port with enabled '
'reloader if the Python interpreter does '
'not support socket opening by fd.')
# Create and destroy a socket so that any exceptions are
# raised before we spawn a separate Python interpreter and
# lose this ability.
address_family = select_ip_version(hostname, port)
s = socket.socket(address_family, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((hostname, port))
if hasattr(s, 'set_inheritable'):
s.set_inheritable(True)
# If we can open the socket by file descriptor, then we can just
# reuse this one and our socket will survive the restarts.
if can_open_by_fd:
os.environ['WERKZEUG_SERVER_FD'] = str(s.fileno())
s.listen(LISTEN_QUEUE)
log_startup(s)
else:
s.close()
from ._reloader import run_with_reloader
run_with_reloader(inner, extra_files, reloader_interval,
reloader_type)
else:
inner()
这是run_simple()的主要作用部分,前两个判断语句是对debug模式以及静态文件的包装。ShareDataMiddleware就是一个中间件,这里是起到吧文件转换为服务器可接受的Response形式的作用。
use_reloader 用于决定当app代码改变时是否要重启服务器,若是True,则他会建立一个socket,其中的can_open_by_fd由socket中是否由fromfd特征决定,如果可以就将fd储存在环境变量中以便重启后的复用,socket开始监听,而后就调用run_with_reloader,它也接受了函数inner.可以看出无论use_reloader是不是True时,都会调用函数内部的inner函数,在inner函数内,在环境中WERKZEUG_SERVER_FD这个key储存了可以复用的socket,若没有就设为None,然后就调用函数make_server,这根据参数process和threads选择合适的服务器,取得服务器对象后,就调用方法run_forever,这服务器也就启动了。,werkzeug提供了多种可选的服务器,这里是一个基本的单线程单进程服务器
class BaseWSGIServer(HTTPServer, object): """Simple single-threaded, single-process WSGI server.""" multithread = False multiprocess = False request_queue_size = LISTEN_QUEUE def __init__(self, host, port, app, handler=None, passthrough_errors=False, ssl_context=None, fd=None): if handler is None: handler = WSGIRequestHandler self.address_family = select_ip_version(host, port) if fd is not None: real_sock = socket.fromfd(fd, self.address_family, socket.SOCK_STREAM) port = 0 HTTPServer.__init__(self, (host, int(port)), handler) self.app = app self.passthrough_errors = passthrough_errors self.shutdown_signal = False self.host = host self.port = port # Patch in the original socket. if fd is not None: self.socket.close() self.socket = real_sock self.server_address = self.socket.getsockname() if ssl_context is not None: if isinstance(ssl_context, tuple): ssl_context = load_ssl_context(*ssl_context) if ssl_context == 'adhoc': ssl_context = generate_adhoc_ssl_context() # If we are on Python 2 the return value from socket.fromfd # is an internal socket object but what we need for ssl wrap # is the wrapper around it :( sock = self.socket if PY2 and not isinstance(sock, socket.socket): sock = socket.socket(sock.family, sock.type, sock.proto, sock) self.socket = ssl_context.wrap_socket(sock, server_side=True) self.ssl_context = ssl_context else: self.ssl_context = None def log(self, type, message, *args): _log(type, message, *args) def serve_forever(self): self.shutdown_signal = False try: HTTPServer.serve_forever(self) except KeyboardInterrupt: pass finally: self.server_close() def handle_error(self, request, client_address): if self.passthrough_errors: raise return HTTPServer.handle_error(self, request, client_address) def get_request(self): con, info = self.socket.accept() return con, info
这个服务器继承了基本的HTTPServer,HTTPServer可以在制定的端口接受数据并处理将结果传递给RequestHandlerClass,具体可以看官方文档https://docs.python.org/2/library/basehttpserver.html
在代码中,request_queue_size制定了请求队列的最大连接数,在__init__函数中handler是请求的处理器,若参数中为提供,就设为默认值,而后就是得到可用的socket,ssl_context主要是帮助实现SSL。另外一些就是简单重写了HTTPServer的方法,在serve_forever中还是调用了HTTPServer的方法来实现服务器功能。
综上就是flask一个基本服务器的实现,在其中可以看到如何为简单的服务器添加多种功能,如SSL,socket的复用,服务器的重载等,接下来就是reloader以及SSL的实现