您好,登錄后才能下訂單哦!
flask是一個純python實現的開源microframework,使用flask可以像java的spring boot一樣快速開始基于RestApi的微服務開發。
概要信息
事前準備
python和pip
[root@liumiaocn ~]# python --version Python 2.7.5 [root@liumiaocn ~]# pip --version pip 10.0.1 from /usr/lib/python2.7/site-packages/pip (python 2.7) [root@liumiaocn ~]#
安裝flask
[root@liumiaocn ~]# pip install flask Collecting flask Downloading https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl (91kB) 100% |################################| 92kB 92kB/s Collecting Jinja2>=2.10 (from flask) Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB) 100% |################################| 133kB 210kB/s Collecting itsdangerous>=0.24 (from flask) Downloading https://files.pythonhosted.org/packages/dc/b4/a60bcdba945c00f6d608d8975131ab3f25b22f2bcfe1dab221165194b2d4/itsdangerous-0.24.tar.gz (46kB) 100% |################################| 51kB 469kB/s Requirement already satisfied: Werkzeug>=0.14 in /usr/lib64/python2.7/site-packages (from flask) (0.14.1) Collecting click>=5.1 (from flask) Downloading https://files.pythonhosted.org/packages/34/c1/8806f99713ddb993c5366c362b2f908f18269f8d792aff1abfd700775a77/click-6.7-py2.py3-none-any.whl (71kB) 100% |################################| 71kB 229kB/s Collecting MarkupSafe>=0.23 (from Jinja2>=2.10->flask) Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz Building wheels for collected packages: itsdangerous, MarkupSafe Running setup.py bdist_wheel for itsdangerous ... done Stored in directory: /root/.cache/pip/wheels/2c/4a/61/5599631c1554768c6290b08c02c72d7317910374ca602ff1e5 Running setup.py bdist_wheel for MarkupSafe ... done Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46 Successfully built itsdangerous MarkupSafe Installing collected packages: MarkupSafe, Jinja2, itsdangerous, click, flask Successfully installed Jinja2-2.10 MarkupSafe-1.0 click-6.7 flask-1.0.2 itsdangerous-0.24 [root@liumiaocn ~]
安裝確認
[root@liumiaocn ~]# pip show flask Name: Flask Version: 1.0.2 Summary: A simple framework for building complex web applications. Home-page: https://www.palletsprojects.com/p/flask/ Author: Armin Ronacher Author-email: armin.ronacher@active-4.com License: BSD Location: /usr/lib64/python2.7/site-packages Requires: Jinja2, itsdangerous, Werkzeug, click Required-by: [root@liumiaocn ~]#
flask安裝之后,也可以用如下方法進行確認
[root@liumiaocn ~]# which flask /usr/bin/flask [root@liumiaocn ~]# flask --version Flask 1.0.2 Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] [root@liumiaocn ~]#
第一個HelloWorld
代碼示例
[root@liumiaocn ~]# cat hello_flask.py from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" [root@liumiaocn ~]#
執行&確認
[root@liumiaocn ~]# FLASK_APP=hello_flask.py flask run * Serving Flask app "hello_flask.py" * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
通過curl進行結果確認:
[root@liumiaocn ~]# curl http://localhost:5000 Hello World![root@liumiaocn ~]#
代碼示例
另外也可以通過使用app.run方法啟動服務:
[root@liumiaocn ~]# cat hello_flask_2.py from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() [root@liumiaocn ~]#
執行&確認
[root@liumiaocn ~]# python hello_flask_2.py * Serving Flask app "hello_flask_2" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
通過curl進行結果確認:
[root@liumiaocn ~]# curl http://localhost:5000 Hello World![root@liumiaocn ~]#
外部可訪問的設定方法
可以通過如下方式進行設定可從網絡的其他機器對此服務進行訪問:
app.run(host='0.0.0.0')
設定端口號
缺省使用5000端口進行監聽,如果需要修改,可以使用如下方式:
app.run(port=6000)
設定debug模式
可以使用如下方式打開Debug模式
app.debug = True app.run()
或者
app.run(debug=True)
小結
對spring boot了解的開發者可能會發現這里基本和spring的requestmapping幾乎如出一轍,至少在restapi的實現上有相似之處。設定和實現起來非常地方便,這就是flask用于web開發入門的簡單之處。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。如果你想了解更多相關內容請查看下面相關鏈接
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。