您的位置:首页 > 编程语言 > Python开发

Python web 框架 Flask 入门 macOS 下实践记录

2017-11-06 20:06 1001 查看
Flask是一个使用 Python 编写的轻量级 Web 应用框架。其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 ,使用 BSD 授权。
Flask也被称为 “microframework” ,因为它使用简单的核心,用 extension 增加其他功能。Flask没有默认使用的数据库、窗体验证工具。然而,Flask保留了扩增的弹性,可以用Flask-extension加入这些功能:ORM、窗体验证工具、文件上传、各种开放式身份验证技术。最新版本为0.12。
学习环境
macOS sierra 10.12.3
Python 2.7.10 
pip 9.0.1

Flask-0.12.2
安装Flask
Linux或mac下使用以下命令安装:

sudo pip install Flask输出如下:
$ sudo pip install Flask
Password:
The directory '/Users/aven/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/aven/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Flask
Downloading Flask-0.12.2-py2.py3-none-any.whl (83kB)
100% |████████████████████████████████| 92kB 90kB/s
Collecting itsdangerous>=0.21 (from Flask)
Downloading itsdangerous-0.24.tar.gz (46kB)
100% |████████████████████████████████| 51kB 24kB/s
Collecting Werkzeug>=0.7 (from Flask)
Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
100% |████████████████████████████████| 317kB 8.0kB/s
Collecting Jinja2>=2.4 (from Flask)
Downloading Jinja2-2.9.6-py2.py3-none-any.whl (340kB)
100% |████████████████████████████████| 348kB 8.8kB/s
Collecting click>=2.0 (from Flask)
Downloading click-6.7-py2.py3-none-any.whl (71kB)
100% |████████████████████████████████| 71kB 11kB/s
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask)
Downloading MarkupSafe-1.0.tar.gz
Installing collected packages: itsdangerous, Werkzeug, MarkupSafe, Jinja2, click, Flask
Running setup.py install for itsdangerous ... done
Running setup.py install for MarkupSafe ... done
Successfully installed Flask-0.12.2 Jinja2-2.9.6 MarkupSafe-1.0 Werkzeug-0.12.2 click-6.7 itsdangerous-0.24执行 flask 查看是否安装成功:
$ flask
Usage: flask [OPTIONS] COMMAND [ARGS]...

This shell command acts as general utility script for Flask applications.

It loads the application configured (through the FLASK_APP environment
variable) and then provides commands either provided by the application or
Flask itself.

The most useful commands are the "run" and "shell" command.

Example usage:

$ export FLASK_APP=hello.py
$ export FLASK_DEBUG=1
$ flask run

Options:
--version Show the flask version
--help Show this message and exit.

Commands:
run Runs a development server.
shell Runs a shell in the app context.完了,这就装完了,比 Django 好,环境变量都不需要配置。

快速入门 quick start
随便找个目录吧
创建一个文件:hello.py ,内容如下:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
return 'Index Page'

@app.route('/hello')
def hello():
return 'Hello, World'

@app.route('/user/<username>')
def show_user_profile(username):
# show the user profile for that user
return 'User %s' % username

@app.route('/post/<int:post_id>')
def show_post(post_id):
# show the post with the given id, the id is an integer
return 'Post %d' % post_id

@app.route('/buy', methods=['POST'])
def buy():
stripe_token = request.form['stripeToken']

if __name__ == "__main__":
app.run()
打开终端,切换到文件所在的目录,执行以下命令启动服务:
$ export FLASK_APP=hello.py
$ export FLASK_DEBUG=1
$ flask run
打开浏览器访问吧:http://127.0.0.1:5000/ * Serving Flask app "hello"
* Forcing debug mode on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 532-211-424
127.0.0.1 - - [03/Nov/2017 11:23:03] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Nov/2017 11:23:04] "GET /favicon.ico HTTP/1.1" 404 -
* Detected change in '/Users/aven/Documents/flask/hello.py', reloading
* Restarting with stat
* Debugger is active!
* Debugger PIN: 532-211-424
127.0.0.1 - - [03/Nov/2017 11:27:26] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Nov/2017 11:27:28] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Nov/2017 11:45:16] "GET /hello HTTP/1.1" 200 -
127.0.0.1 - - [03/Nov/2017 11:46:12] "GET /user/%E5%BE%AEwx%E7%AC%91 HTTP/1.1" 200 -
127.0.0.1 - - [03/Nov/2017 13:33:14] "GET /user/%E5%BE%AEwx%E7%AC%91a HTTP/1.1" 200 -命令说明:1、设置APP 的启动文件;2、设置为调试模式启动;3、运行;如果你使用Windows操作系统,需要将 export 替换为 set
启动成功后,我们可以根据提示访问:http://127.0.0.1:5000/如果想退出,按 CTRL + C 就可以了。访问:http://127.0.0.1:5000/,我们会看到输出:Index Page访问:http://127.0.0.1:5000/hello,会看到输出:Hello, World访问:http://127.0.0.1:5000/user/%E5%BE%AEwx%E7%AC%91, 会看到输出:User 微wx笑而访问:http://127.0.0.1:5000/buy,我们会收到提示:方法不支持。

让你的服务器对外可见

按上面运行服务器的方式,您将注意到服务器只能从您自己的计算机访问,而不能从网络中的任何其他计算机访问。这是默认值,因为在调试模式下,应用程序的用户可以在计算机上执行任意的Python代码。如果禁用调试器或信任网络上的用户,则可以通过在命令行中添加--host = 0.0.0.0来使服务器公开可用:[python] view plain copyflask run --host=0.0.0.0  

静态文件

动态Web应用程序也需要静态文件。这通常是放 CSS和JavaScript文件的地方。只需在程序包中或在模块目录创建一个名为static的文件夹,它将在应用程序的/static处可用。把一些静态文件放在这下面,然后通过 http://127.0.0.1:5000/static/index.html 这样就可以访问了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: