A simple Flask server
1 | from flask import Flask |
1 Flask Request Entry
This is a simple werkzeug server
1 | from werkzeug.wrappers import Request, Response |
app.run()
: bases onwerkzeug.serving.run_simple(host, port, self, **options)
. So once we visit its route, the entry of Flask application is app(), which is theFlask.__call__
.Flask.__call__
: use wsgi_appreturn self.wsgi_app(environ, start_response)
- In the end, the real entry is
wsgi_app(environ, start_response)
2 Route Register
1 |
|
This Python Decorator is equivalant to
1 | def hello_world(): |
app.route()
: base on self.add_url_rule(rule, endpoint, f, **options). It binds a function with a url. So these two are equivalantapp.add_url_rule("/", hello_world.__name__, hello_world)
app.route("/")(hello_world)
add_url_rule(rule, endpoint, view_func)
:rule = self.url_rule_class(rule, methods=methods, **options)
: combine url, methods(post, get…), endpoint togetherself.url_map.add(rule)
: this url_map is werkzeug.routing.Mapself.view_function[endpoint] = view_func
, it binds endpoint with function and is used when dispatching request
3 Dispatching Request
From Flask Request Entry, the real request entry is wsgi_app(environ, start_response)
, What does it do with the Routing system ?
- push
request_context
into context Flask.dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
Finally, it is equal to return hello_world()