Flask 4 拓展

NOTE

1.Flask被设计为可拓展模式,所以没有提供如数据库和用户认证等重要的功能,允许开发者按需开发。

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

安装flask-script:

pip install flask-script

将命令行解析功能添加到hello.py程序:

#!/usr/bin/env python

from flask import Flask
from flask.ext.script import Manager

app = Flask(__name__)
manager = Manager(app)

@app.route('/')
def index():
    return '<h1> Make P4 Great Again! </h1>'

if __name__ == '__main__':
    # app.run(debug=True)
    manager.run()
(venv) sh-3.2# ./hello_mng.py 
./hello_mng.py:4: ExtDeprecationWarning: Importing flask.ext.script is deprecated, use flask_script instead.
  from flask.ext.script import Manager
usage: hello_mng.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
(venv) sh-3.2# ./hello_mng.py runserver -?
./hello_mng.py:4: ExtDeprecationWarning: Importing flask.ext.script is deprecated, use flask_script instead.
  from flask.ext.script import Manager
usage: hello_mng.py runserver [-?] [-h HOST] [-p PORT] [--threaded]
                              [--processes PROCESSES] [--passthrough-errors]
                              [-d] [-D] [-r] [-R]

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{'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_mng.py runserver',
                        'container': <argparse._ArgumentGroup object at
                        0x10c0f4b50>, 'type': None, 'metavar': None}afe for
                        production use)
  -R, --no-reload       do not monitor Python files for changes

3.在默认情况下,Flask开发服务器监听localhost上的连接,所以只接收来自服务器本身所在计算机的连接。--host参数可以帮助web服务器知晓在哪个网络接口上监听。

(venv) sh-3.2# ./hello_mng.py runserver --host 0.0.0.0
./hello_mng.py:4: ExtDeprecationWarning: Importing flask.ext.script is deprecated, use flask_script instead.
  from flask.ext.script import Manager
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

你可以在公网IP的5000端口上进行请求。

2017/2/17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值