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端口上进行请求。