Flask<2>--Dispatching-Request

A simple Flask server

1
2
3
4
5
6
7
8
9
from flask import Flask
app = Flask(__name__)

@app.route('/') # -------------------- 2
def hello_world():
return 'Hello World!'

if __name__ == '__main__':
app.run() # ---------------------- 1

1 Flask Request Entry

This is a simple werkzeug server

1
2
3
4
5
6
7
8
9
10
from werkzeug.wrappers import Request, Response

@Request.application
def hello(request):
return Response('Hello World!')

if __name__ == '__main__':
from werkzeug.serving import run_simple
# listen to `http://localhost:4000/` and do a hello() once we visit it.
run_simple('localhost', 4000, hello)
  • app.run(): bases on werkzeug.serving.run_simple(host, port, self, **options). So once we visit its route, the entry of Flask application is app(), which is the Flask.__call__.
  • Flask.__call__: use wsgi_app
    • return self.wsgi_app(environ, start_response)
  • In the end, the real entry is wsgi_app(environ, start_response)

2 Route Register

1
2
3
@app.route('/')
def hello_world():
return 'Hello World!'

This Python Decorator is equivalant to

1
2
3
def hello_world():
return "Hello World"
app.route("/")(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 equivalant

    • app.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 together
    • self.url_map.add(rule): this url_map is werkzeug.routing.Map
    • self.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 ?

  1. push request_context into context
  2. Flask.dispatch_request
    • return self.view_functions[rule.endpoint](**req.view_args)

Finally, it is equal to return hello_world()