flask web学习笔记>>>script扩展

flask扩展

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

pip install flask-script

使用,注意名字已经改变了

from flask_script import Manager
manager = Manager(app)
# ...
if __name__ == '__main__':
	manager.run()

专为 Flask 开发的扩展都暴漏在 flask.ext 命名空间下。 Flask-Script 输出了一个名为Manager 的类,可从 flask.ext.script 中引入。

这个扩展的初始化方法也适用于其他很多扩展: 把程序实例作为参数传给构造函数,初始化主类的实例。 创建的对象可以在各个扩展中使用。在这里,服务器由 manager.run() 启动,启动后就能解析命令行了。

先修改hello.py程序,shell窗口执行

>python hello.py runserver --help

shell窗口提示

usage: helloworld.py runserver [-?] [-h HOST] [-p PORT] [--threaded]
                               [--processes PROCESSES] [--passthrough-errors]
                               [-d] [-D] [-r] [-R] [--ssl-crt SSL_CRT]
                               [--ssl-key SSL_KEY]

Runs the Flask development server i.e. 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% safe for
                        production use)
  -R, --no-reload       do not monitor Python files for changes
  --ssl-crt SSL_CRT     Path to ssl certificate
  --ssl-key SSL_KEY     Path to ssl key

  • –host 参数是个很有用的选项,它告诉 Web 服务器在哪个网络接口上监听来自客户端的连接。默认情况下, Flask 开发 Web 服务器监听 localhost 上的连接,所以只接受来自服务器所在计算机发起的连接。下述命令让 Web 服务器监听公共网络接口上的连接, 允许同网中的其他计算机连接服务器:
python hello.py runserver --host 0.0.0.0
  • 现在, Web 服务器可使用 http://a.b.c.d:5000/ 网络中的任一台电脑进行访问,其中“ a.b.c.d”是服务器所在计算机的外网 IP 地址。
 * Serving Flask app "helloworld" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

你可能感兴趣的:(flask,web,python)