2.6、Flask扩展

Flask 被设计为可扩展形式,故而没有提供一些重要的功能,例如数据库和用户认证,所以开发者可以自由选择最适合程序的包,或者按需求自行开发。

社区成员开发了大量不同用途的扩展,如果这还不能满足需求,你还可使用所有 Python 标准包或代码库。为了让你知道如何把扩展整合到程序中,接下来我们将在 hello.py 中添加一个扩展,使用命令行参数增强程序的功能。

使用Flask-Script支持命令行选项

Flask 的开发 Web 服务器支持很多启动设置选项,但只能在脚本中作为参数传给 app.run()函数。这种方式并不十分方便,传递设置选项的理想方式是使用命令行参数。

Flask-Script 是一个 Flask 扩展,为 Flask 程序添加了一个命令行解析器。Flask-Script 自带了一组常用选项,而且还支持自定义命令。

Flask-Script 扩展使用 pip 安装:

(venv)[user@localhost test]$ pip install flask-script

示例 2-3 显示了把命令行解析功能添加到 hello.py 程序中时需要修改的地方。

示例 2-3 hello.py:使用 Flask-Script

 

from flask_script import Manager

manager = Manager(app)

 

# ...

 

if __name__ == '__main__':

    manager.run()

专为 Flask 开发的扩展都暴漏在 flask.ext 命名空间下。Flask-Script 输出了一个名为Manager 的类,可从 flask_script 中引入。这个扩展的初始化方法也适用于其他很多扩展:把程序实例作为参数传给构造函数,初始化主类的实例。创建的对象可以在各个扩展中使用。在这里,服务器由 manager.run() 启动,启动后就能解析命令行了。

这样修改之后,程序可以使用一组基本命令行选项。现在运行 hello.py,会显示一个用法

消息:

(venv)[user@localhost test]$ python hello.py

usage: hello.py [-?] {shell,runserver} ...

 

positional arguments:

  {shell,runserver}

    shell            Runs a Python shell inside Flask application context.

    runserver        Runs the Flask development server i.e. app.run()

 

optional arguments:

  -?, --help         show this help message and exit

shell 命令用于在程序的上下文中启动 Python shell 会话。你可以使用这个会话中运行维护任务或测试,还可调试异常。

顾名思义,runserver 命令用来启动 Web 服务器。运行 python hello.py runserver 将以调试模式启动 Web 服务器,但是我们还有很多选项可用:

 

(venv)[user@localhost test]$ python hello.py runserver --help

usage: hello.py runserver [-?] [-h HOST] [-p PORT] [--threaded]

                          [--processes PROCESSES] [--passthrough-errors] [-d]

                          [-D] [-r] [-R]

 

Runs the Flask development server i.e. app.run()  # 运行 Flask 开发服务器:app.run()

 

optional arguments:

  -?, --help            show this help message and exit   # 显示帮助信息并退出

  -h HOST, --host HOST

  -p PORT, --port PORT

  --threaded

  --processes PROCESSES

  --passthrough-errors

  -d, --debug           enable the Werkzeug debugger (DO NOT use in production

                        code)

  -D, --no-debug        disable the Werkzeug debugger

  -r, --reload          monitor Python files for changes (not 100{'const':

                        True, 'help': 'monitor Python files for changes (not

                        100% safe for production use)', 'option_strings':

                        ['-r', '--reload'], 'dest': 'use_reloader',

                        'required': False, 'nargs': 0, 'choices': None,

                        'default': None, 'prog': 'hello.py runserver',

                        'container': <argparse._ArgumentGroup object at

                        0x20260d0>, 'type': None, 'metavar': None}afe for

                        production use)

  -R, --no-reload       do not monitor Python files for changes

--host 参数是个很有用的选项,它告诉 Web 服务器在哪个网络接口上监听来自客户端的连接。默认情况下,Flask 开发 Web 服务器监听 localhost 上的连接,所以只接受来自服务器所在计算机发起的连接。下述命令让 Web 服务器监听公共网络接口上的连接,允许同网中的其他计算机连接服务器:

(venv)[user@localhost test]$ python hello.py runserver --host 0.0.0.0

 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

现在,Web 服务器可使用 http://a.b.c.d:5000/ 网络中的任一台电脑进行访问,其中“a.b.c.d”是服务器所在计算机的外网 IP 地址。

 

说明:若外部主机无法访问,检查本机的防火墙配置,可以关闭防火墙试一下

 

posted @ 2017-03-29 18:20  哈喽哈喽111111  阅读(1305)  评论(0编辑  收藏  举报