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

时间:2022-11-16 23:17:01

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

概要信息

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

事前准备

python和pip

?
1
2
3
4
5
[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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[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 ~]

安装确认

?
1
2
3
4
5
6
7
8
9
10
11
12
[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安装之后,也可以用如下方法进行确认

?
1
2
3
4
5
6
7
[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

代码示例

?
1
2
3
4
5
6
7
[root@liumiaocn ~]# cat hello_flask.py
from flask import flask
app = flask(__name__)
@app.route("/")
def hello():
  return "hello world!"
[root@liumiaocn ~]#

执行&确认

?
1
2
3
4
5
6
7
[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进行结果确认:

?
1
2
[root@liumiaocn ~]# curl http://localhost:5000
hello world![root@liumiaocn ~]#

代码示例

另外也可以通过使用app.run方法启动服务:

?
1
2
3
4
5
6
7
8
9
[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 ~]#

执行&确认

?
1
2
3
4
5
6
7
[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进行结果确认:

?
1
2
[root@liumiaocn ~]# curl http://localhost:5000
hello world![root@liumiaocn ~]#

外部可访问的设定方法

可以通过如下方式进行设定可从网络的其他机器对此服务进行访问:

?
1
app.run(host='0.0.0.0')

设定端口号

缺省使用5000端口进行监听,如果需要修改,可以使用如下方式:

?
1
app.run(port=6000)

设定debug模式

可以使用如下方式打开debug模式

?
1
2
app.debug = true
app.run()

或者

?
1
app.run(debug=true)

小结

对spring boot了解的开发者可能会发现这里基本和spring的requestmapping几乎如出一辙,至少在restapi的实现上有相似之处。设定和实现起来非常地方便,这就是flask用于web开发入门的简单之处。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/liumiaocn/article/details/80720914