Python编程中flask的简介与简单使用

flask是一个纯python实现的开源microframework,使用flask可以像java的spring boot一样快速开始基于RestApi的微服务开发。

概要信息

Python编程中flask的简介与简单使用

事前准备

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: [email protected]
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开发入门的简单之处。