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()
自定制正則路由匹配

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM