集成Python Shell

每次启动shell会话都要导入Python相关对象(数据库实例和模型),这是件十分枯燥的工作。为了避免一直重复导入,我们可以做些配置,让flask-script的shell命令自动导入特定的对象。

Flask的开发Web服务器支持很多启动设置选项,但只能在脚本中作为参数传给app.run()函数。这种方式并不十分方便,传递设置选项的理想方式是使用命令行参数。
Flask-script是一个Flask扩展,为Flask程序添加了一个命令行解析器。Flask-script自带了一组常用选项,而且还支持自定义命令。

Flask-Script扩展使用pip安装:

(flask)$ pip install flask-script

创建Flask-script实例

首先,创建一个Python模块运行你的命令脚本。可以任意起名,例如manage.py。
在你的manage.py文件中,必须有一个Manager实例。Manager类将追踪所有的在命令行中调用的命令和处理过程的调用运行情况:

from flask.ext.script import Manager

app = Flask(__name__)
# configure your app

manager = Manager(app)

if __name__ == "__main__":
    manager.run()

运行Flask-script命令

调用 manager.run()将启动Manager实例接收命令行中的命令。

现在运行manage.py会显示一个用法消息:

(flask)$ python manage.py 
usage: manage.py [-?] {test,shell,db,runserver} ...

positional arguments:
  {test,shell,db,runserver}
    shell               Runs a Python shell inside Flask application context.
                        (在Flask应用上下文中运行Python shell)
    db                  Perform database migrations
    runserver           Runs the Flask development server i.e. app.run()
                        (运行Flask开发服务器: app.run())

optional arguments:
  -?, --help            show this help message and exit

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

启动WEB服务器

(flask)$ python manage.py runserver --help
usage: manage.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': 'manage.py runserver',
                        'container': <argparse._ArgumentGroup object at
                        0x7f6c48b20150>, 'type': None, 'metavar': None}afe for
                        production use)
  -R, --no-reload       do not monitor Python files for changes

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

(flask) $python manage.py runserver --host 0.0.0.0
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat

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

如果你使用Pycharm来编程,那么你需要设置Pycharm的启动项。Run->Edit Configurations,弹出如下窗口,并按照窗口内容设置。



链接:http://www.jianshu.com/p/ee500190dc97
來源:简书

posted on 2017-07-29 15:20  lpx15312  阅读(846)  评论(0编辑  收藏  举报

导航