Flask基础原理

一、Flask简介

Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架。

Werkzeug的本质是Socket服务端,用于接收http请求并对请求进行预处理,然后触发Flask框架,开发人员基于Flask框架提供的功能对请求进行相应的处理,并返回给用户,如果要返回给用户复杂的内容时,需要借助jinja2模板来实现对模板的处理,即:将模板和数据进行渲染,将渲染后的字符串返回给用户浏览器。

“微”(micro) 并不表示你需要把整个 Web 应用塞进单个 Python 文件,也不意味着 Flask 在功能上有所欠缺。微框架中的“微”意味着 Flask 旨在保持核心简单而易于扩展。Flask 不会替你做出太多决策——比如使用何种数据库。而那些 Flask 所选择的——比如使用何种模板引擎——则很容易替换。除此之外的一切都可由你掌握。

默认情况下,Flask 不包含数据库抽象层、表单验证,或是其它任何已有多种库可以胜任的功能。然而,Flask 支持用扩展来给应用添加这些功能,如同是 Flask 本身实现的一样。众多的扩展提供了数据库集成、表单验证、上传处理、各种各样的开放认证技术等功能。Flask 也许是“微小”的,但它已准备好在需求繁杂的生产环境中投入使用。

二、werkzeug模块的应用

 1 from werkzeug.wrappers import Request, Response
 2 
 3 @Request.application
 4 def hello(request):
 5     return Response('Hello World!')
 6 
 7 if __name__ == '__main__':
 8     from werkzeug.serving import run_simple
 9     run_simple('localhost', 4000, hello)
10 
11 # 通过以上程序,可以运行起一个web服务,Flask的socket服务也是通过这个方法运行的

三、Flask的基本安装与使用

 1 pip3 install flask
 2 
 3 from flask import Flask
 4 app = Flask(__name__)
 5  
 6 @app.route('/')
 7 def hello_world():
 8     return 'Hello World!'
 9  
10 if __name__ == '__main__':
11     app.run()
12 
13 # 开启了一个web服务,根目录会返回Hello World字符串

四、Flask的配置文件

 1 flask中的配置文件是一个flask.config.Config对象(继承字典)
  settings.py文件默认路径要放在程序root_path目录,如果instance_relative_config为True,则就是instance_path目录

  默认配置为:
2 { 3 'DEBUG': get_debug_flag(default=False), 是否开启Debug模式 4 'TESTING': False, 是否开启测试模式 5 'PROPAGATE_EXCEPTIONS': None, 6 'PRESERVE_CONTEXT_ON_EXCEPTION': None, 7 'SECRET_KEY': None, 8 'PERMANENT_SESSION_LIFETIME': timedelta(days=31), 9 'USE_X_SENDFILE': False, 10 'LOGGER_NAME': None, 11 'LOGGER_HANDLER_POLICY': 'always', 12 'SERVER_NAME': None, 13 'APPLICATION_ROOT': None, 14 'SESSION_COOKIE_NAME': 'session', 15 'SESSION_COOKIE_DOMAIN': None, 16 'SESSION_COOKIE_PATH': None, 17 'SESSION_COOKIE_HTTPONLY': True, 18 'SESSION_COOKIE_SECURE': False, 19 'SESSION_REFRESH_EACH_REQUEST': True, 20 'MAX_CONTENT_LENGTH': None, 21 'SEND_FILE_MAX_AGE_DEFAULT': timedelta(hours=12), 22 'TRAP_BAD_REQUEST_ERRORS': False, 23 'TRAP_HTTP_EXCEPTIONS': False, 24 'EXPLAIN_TEMPLATE_LOADING': False, 25 'PREFERRED_URL_SCHEME': 'http', 26 'JSON_AS_ASCII': True, 27 'JSON_SORT_KEYS': True, 28 'JSONIFY_PRETTYPRINT_REGULAR': True, 29 'JSONIFY_MIMETYPE': 'application/json', 30 'TEMPLATES_AUTO_RELOAD': None, 31 } 32 33 方式一: 34 app.config['DEBUG'] = True 35 36 PS: 由于Config对象本质上是字典,所以还可以使用app.config.update(...) 37 38 方式二: 39 app.config.from_pyfile("python文件名称") 40 如: 41 settings.py 42 DEBUG = True 43 44 app.config.from_pyfile("settings.py") 45 46 app.config.from_envvar("环境变量名称") 47 环境变量的值为python文件名称名称,内部调用from_pyfile方法 48 49 50 app.config.from_json("json文件名称") 51 JSON文件名称,必须是json格式,因为内部会执行json.loads 52 53 app.config.from_mapping({'DEBUG':True}) 54 字典格式 55 56 app.config.from_object("python类或类的路径") 57 58 app.config.from_object('pro_flask.settings.TestingConfig') 59 60 settings.py 61 62 class Config(object): 63 DEBUG = False 64 TESTING = False 65 DATABASE_URI = 'sqlite://:memory:' 66 67 class ProductionConfig(Config): 68 DATABASE_URI = 'mysql://user@localhost/foo' 69 70 class DevelopmentConfig(Config): 71 DEBUG = True 72 73 class TestingConfig(Config): 74 TESTING = True 75 76 PS: 从sys.path中已经存在路径开始写

五、路由系统

  • @app.route('/user/<username>')
  • @app.route('/post/<int:post_id>')
  • @app.route('/post/<float:post_id>')
  • @app.route('/post/<path:path>')
  • @app.route('/login', methods=['GET', 'POST'])

常用路由系统有以上五种,所有的路由系统都是基于一下对应关系来处理:

1 DEFAULT_CONVERTERS = {
2     'default':          UnicodeConverter,
3     'string':           UnicodeConverter,
4     'any':              AnyConverter,
5     'path':             PathConverter,
6     'int':              IntegerConverter,
7     'float':            FloatConverter,
8     'uuid':             UUIDConverter,
9 }
  1 def auth(func):
  2             def inner(*args, **kwargs):
  3                 print('before')
  4                 result = func(*args, **kwargs)
  5                 print('after')
  6                 return result
  7 
  8         return inner
  9 
 10         @app.route('/index.html',methods=['GET','POST'],endpoint='index')
 11         @auth
 12         def index():
 13             return 'Index'
 14 
 15  16         
 17         def index():
 18             return "Index"
 19 
 20         self.add_url_rule(rule='/index.html', endpoint="index", view_func=index, methods=["GET","POST"])
 21         or
 22         app.add_url_rule(rule='/index.html', endpoint="index", view_func=index, methods=["GET","POST"])
 23         app.view_functions['index'] = index
 24 
 25 
 26  27         def auth(func):
 28             def inner(*args, **kwargs):
 29                 print('before')
 30                 result = func(*args, **kwargs)
 31                 print('after')
 32                 return result
 33 
 34         return inner
 35 
 36         class IndexView(views.View):
 37             methods = ['GET']
 38             decorators = [auth, ]
 39 
 40             def dispatch_request(self):
 41                 print('Index')
 42                 return 'Index!'
 43 
 44         app.add_url_rule('/index', view_func=IndexView.as_view(name='index'))  # name=endpoint
 45 
 46 
 47 
 48  49 
 50 
 51         class IndexView(views.MethodView):
 52             methods = ['GET']
 53             decorators = [auth, ]
 54 
 55             def get(self):
 56                 return 'Index.GET'
 57 
 58             def post(self):
 59                 return 'Index.POST'
 60 
 61 
 62         app.add_url_rule('/index', view_func=IndexView.as_view(name='index'))  # name=endpoint
 63 
 64 
 65 
 66 
 67         @app.route和app.add_url_rule参数:
 68             rule,                       URL规则
 69             view_func,                  视图函数名称
 70             defaults=None,              默认值,当URL中无参数,函数需要参数时,使用defaults={'k':'v'}为函数提供参数
 71             endpoint=None,              名称,用于反向生成URL,即: url_for('名称')
 72             methods=None,               允许的请求方式,如:["GET","POST"]
 73             
 74 
 75             strict_slashes=None,        对URL最后的 / 符号是否严格要求,
 76                                         如:
 77                                             @app.route('/index',strict_slashes=False),
 78                                                 访问 http://www.xx.com/index/ 或 http://www.xx.com/index均可
 79                                             @app.route('/index',strict_slashes=True)
 80                                                 仅访问 http://www.xx.com/index 
 81             redirect_to=None,           重定向到指定地址
 82                                         如:
 83                                             @app.route('/index/<int:nid>', redirect_to='/home/<nid>')
 84  85                                             def func(adapter, nid):
 86                                                 return "/home/888"
 87                                             @app.route('/index/<int:nid>', redirect_to=func)
 88             subdomain=None,             子域名访问
 89                                                 from flask import Flask, views, url_for
 90 
 91                                                 app = Flask(import_name=__name__)
 92                                                 app.config['SERVER_NAME'] = 'wupeiqi.com:5000'
 93 
 94 
 95                                                 @app.route("/", subdomain="admin")
 96                                                 def static_index():
 97                                                     """Flask supports static subdomains
 98                                                     This is available at static.your-domain.tld"""
 99                                                     return "static.your-domain.tld"
100 
101 
102                                                 @app.route("/dynamic", subdomain="<username>")
103                                                 def username_index(username):
104                                                     """Dynamic subdomains are also supported
105                                                     Try going to user1.your-domain.tld/dynamic"""
106                                                     return username + ".your-domain.tld"
107 
108 
109                                                 if __name__ == '__main__':
110                                                     app.run()
111         
注册路由方法
 1 from flask import Flask, views, url_for
 2             from werkzeug.routing import BaseConverter
 3 
 4             app = Flask(import_name=__name__)
 5 
 6 
 7             class RegexConverter(BaseConverter):
 8                 """
 9                 自定义URL匹配正则表达式
10                 """
11                 def __init__(self, map, regex):
12                     super(RegexConverter, self).__init__(map)
13                     self.regex = regex
14 
15                 def to_python(self, value):
16                     """
17                     路由匹配时,匹配成功后传递给视图函数中参数的值
18                     :param value: 
19                     :return: 
20                     """
21                     return int(value)
22 
23                 def to_url(self, value):
24                     """
25                     使用url_for反向生成URL时,传递的参数经过该方法处理,返回的值用于生成URL中的参数
26                     :param value: 
27                     :return: 
28                     """
29                     val = super(RegexConverter, self).to_url(value)
30                     return val
31 
32             # 添加到flask中
33             app.url_map.converters['regex'] = RegexConverter
34 
35 
36             @app.route('/index/<regex("d+"):nid>')
37             def index(nid):
38                 print(url_for('index', nid='888'))
39                 return 'Index'
40 
41 
42             if __name__ == '__main__':
43                 app.run()
自定制正则路由匹配
原文地址:https://www.cnblogs.com/eric_yi/p/8184018.html