简介

Flask-Scropt插件为在Flask里编写额外的脚本提供了支持。这包括运行一个开发服务器,一个定制的Python命令行,用于执行初始化数据库、定时任务和其他属于web应用之外的命令行任务的脚本。

安装

用命令pip和easy_install安装:

pip install Flask-Script

从github下载最新版本,源码编译安装:

git clone https://github.com/smurfix/flask-script.git
cd flask-script
python setup.py develop

创建并运行命令行

第一步:实例化manage对象

需要创建一个可以运行你脚本命令的Python模块。你可以随意命名它。我这里就以manage.py为例。

在manage.py文件中,需要先创建一个Manager实例。Manager类会跟踪所有的命令和命令行调用的参数:

from flask_script import Manager

app = Flask(__name__)
# configure your app manager = Manager(app) if __name__ == "__main__":
manager.run()

调用manager.run()方法初始化Mnager实例来接收命令行输入。

此时,已经可以通过命令启动项目了,如下:

python manage.py runserver

项目会以:Running on http://127.0.0.1:5000/ 的方式启动,

如需指定ip和端口:

python manage.py runserver -h 127.0.0.1 -p 8090

项目则会以:Running on http://127.0.0.1:8090/ 的方式启动,其实也是可以指定IP的,只是本质也是127.0.0.1

第二步:创建添加自定义命令

创建自定义命令有三种方法:

  • 定义Command类的子类
  • 使用@command装饰器
  • 使用@option装饰器

(1) 定义Command类的子类

为了简单,我们就创建一个hello命令来输出“hello world”:

from flask_script import Command

class Hello(Command):
"prints hello world" def run(self):
print "hello world"

接下来我们需要把命令添加到Mannager实例:

manager.add_command('hello', Hello())

完整代码如下:

from flask_script import Manager,Command
from flask import Flask
app = Flask(__name__) manager = Manager(app) class hello(Command):
"prints hello world"
def run(self):
print("hello world") manager.add_command('hello', hello()) if __name__ == "__main__":
manager.run()

使用:

在命令行运行如下命令:
(1)$python manage.py hello
hello world
(2)$python manage.py
usage: manage.py [-?] {hello,shell,runserver} ... positional arguments:
{hello,shell,runserver}
hello prints hello world
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 也可以通过把包含Command实例的字典作为manager.run()的参数:
manager.run({'hello' : Hello()})

(2)使用@command装饰器

对于简单的命令,我们可以使用属于Manager实例的@command装饰器。

@manager.command
def hello():
"Just say hello"
print("hello")

其使用方法和前面一样。

(3)使用@option装饰器

如何需要通过命令行进行比较复杂的控制,可以使用Manager实例的@option装饰器。

@manager.option('-n', '--name', help='Your name')
def hello(name):
print("hello", name)

使用

python manage.py -n '付勇'

  则会输出:‘hello 付勇’

 
 
 

Flask之flask-script 指定端口的更多相关文章

  1. Flask01 初识flask、flask配置

    1 什么是flask Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 . 百度百科:点击前往 中文文档: ...

  2. Flask入门 flask结构 url_for 重定向(一)

    Flask入门(一) 1 安装虚拟环境Mac,linux sudo pip install virtualenv ​ ubuntu系统 sudo apt-get install python-virt ...

  3. Python flask 基于 Flask 提供 RESTful Web 服务

    转载自 http://python.jobbole.com/87118/ 什么是 REST REST 全称是 Representational State Transfer,翻译成中文是『表现层状态转 ...

  4. [Flask]学习Flask第三天笔记总结

    from flask import Flask,render_template,request from others import checkLogin app = Flask(__name__) ...

  5. flask框架----flask基础

    知识点回顾 1.flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 2.实例化Flask对象,里面是有参数的 app = Flask(__name__,templ ...

  6. 【Python】【Flask】Flask 后台发送html页面多种方法

    1.使用模板: @app.route('/') def home(): return render_template("homepage.html")#homepage.html在 ...

  7. flask中Flask()和Blueprint() flask中的g、add_url_rule、send_from_directory、static_url_path、static_folder的用法

    1.Blueprint()在蓝本注册函数register_blueprint()中,第一个参数为所注册的蓝本名称.当我们在应用对象上注册一个蓝图时,需要指定一个url_prefix关键字 参数(这个参 ...

  8. Flask之Flask实例有哪些参数

    常用的参数应用实例 from flask import Flask, render_template, url_for, session, request, redirect app = Flask( ...

  9. 【Flask】Flask Cookie操作

    ### 什么是cookie:在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是为了解决这个问题,第 ...

随机推荐

  1. 【SQL Server数据迁移】把csv文件中的数据导入SQL Server的方法

    [sql] view plaincopy --1.修改系统参数 --修改高级参数 sp_configure 'show advanced options',1 go --允许即席分布式查询 sp_co ...

  2. 8-python模拟登入(无验证码)

    方式: 1.手动登入,获取cookie 2.使用cookielib库 和 HTTPCookieProcessor处理器 #_*_ coding: utf-8 _*_ ''' Created on 20 ...

  3. jqgrid扩展 获取表单数据

    $.fn.GetPostData = function () { var data = {}; var k = false; $(this).find(".datacontrol" ...

  4. Jmeter接口测试-完成任务API

    完成任务 PUT /api/tasks/:task_id 可以完成id为task_id的task,如果动作成功,该接口返回的task的done字段会变成true. 完成任务的api接口测试很简单,因为 ...

  5. LightOJ 1284 Lights inside 3D Grid (数学期望)

    题意:在一个三维的空间,每个点都有一盏灯,开始全是关的.现在每次随机选两个点,把两个点之间的全部点,开关都按一遍,问k次过后开着的灯的期望数量: 析:很容易知道,如果一盏灯被按了奇数次,那么它肯定是开 ...

  6. UVa 1625 Color Length (DP)

    题意:给定两个序列,让你组成一个新的序列,让两个相同字符的位置最大差之和最小.组成方式只能从一个序列前部拿出一个字符放到新序列中. 析:这个题状态表示和转移很容易想到,主要是在处理上面,dp[i][j ...

  7. 云存储上传控件(cloud2)-Xproer.CloudUploader

    版权所有 2009-2016荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  8. Newtonsoft.Json.Linq

    var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...

  9. C# - dynamic 特性

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性. 比如,即使 ...

  10. 类的 where T : class 泛型类型约束

    where T : struct | T必须是一个结构类型where T : class T必须是一个类(class)类型where T : new() | T必须要有一个无参构造函数where T ...