Flask之flask-script 指定端口
简介
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 指定端口的更多相关文章
- Flask01 初识flask、flask配置
1 什么是flask Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 . 百度百科:点击前往 中文文档: ...
- Flask入门 flask结构 url_for 重定向(一)
Flask入门(一) 1 安装虚拟环境Mac,linux sudo pip install virtualenv ubuntu系统 sudo apt-get install python-virt ...
- Python flask 基于 Flask 提供 RESTful Web 服务
转载自 http://python.jobbole.com/87118/ 什么是 REST REST 全称是 Representational State Transfer,翻译成中文是『表现层状态转 ...
- [Flask]学习Flask第三天笔记总结
from flask import Flask,render_template,request from others import checkLogin app = Flask(__name__) ...
- flask框架----flask基础
知识点回顾 1.flask依赖wsgi,实现wsgi的模块:wsgiref,werkzeug,uwsgi 2.实例化Flask对象,里面是有参数的 app = Flask(__name__,templ ...
- 【Python】【Flask】Flask 后台发送html页面多种方法
1.使用模板: @app.route('/') def home(): return render_template("homepage.html")#homepage.html在 ...
- flask中Flask()和Blueprint() flask中的g、add_url_rule、send_from_directory、static_url_path、static_folder的用法
1.Blueprint()在蓝本注册函数register_blueprint()中,第一个参数为所注册的蓝本名称.当我们在应用对象上注册一个蓝图时,需要指定一个url_prefix关键字 参数(这个参 ...
- Flask之Flask实例有哪些参数
常用的参数应用实例 from flask import Flask, render_template, url_for, session, request, redirect app = Flask( ...
- 【Flask】Flask Cookie操作
### 什么是cookie:在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是为了解决这个问题,第 ...
随机推荐
- 2、awk的输出
1.常见的输出格式整理 awk '{print "this is " $1, $2, $1*$2, NR, NF, $NF}' file1 ###字符输出,字段输出,运算输出, ...
- Unity破解不成功解决方案
你是不是遇到过Unity新版本出来的时候就急着使用,但是安装好了,却破解不成功的问题(你之前的版本破解过).这是由于你的注册表没有彻底的删除,接下来我们图解如何清理. 1.卸载以前的版本,卸载完了删除 ...
- QT开发环境
代码实现界面和槽 代码实现界面和槽 在上述工程的dialog.h中添加如下加黑代码: 加入头文件: #include <QLabel> #include <QLineEdit> ...
- shell 字符串中定位字符位置 获取字符位置
linux shell 字符串操作(长度,查找,替换)详解 该博文中描述的如下两个字符串操作, ${string:position} #在$string中, 从位置$position开始提取子串 ${ ...
- [转]windows7远程桌面连接失败:发生身份验证错误。要求的函数不受支持
转至:https://jingyan.baidu.com/article/d169e18604ca86436611d821.html 系统升级后出现远程连接报错,“发生身份验证错误.要求的函数不受支持 ...
- MongoDB整理笔记の安装及配置
1.官网下载 地址:http://www.mongodb.org/downloads mongodb-linux-x86_64-2.4.9.tgz (目前为止,64位最新版本) 2.解压 切换到下载目 ...
- Autofac的Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误解决方案。
在使用Autofac的时候,不给力,看着例子来的,人家没问题,我就报了Autofac.Core.Activators.Reflection.DefaultConstructorFinder错误. 百般 ...
- android android各种应用的许可
android各种应用的许可 在Android的设计中,资源的访问或者网络连接,要得到这些服务都需要声明其访问权限,否则将无法正常工作.在Android中这样的权限有很多种,这里将各类访问权限一一罗列 ...
- 声明函数指针、回调函数、函数对象------c++程序设计基础、编程抽象与算法策略
声明函数指针 #include<iostream> using namespace std; double a(double aa) { return aa; } int main() { ...
- iOS No suitable application records were found. Verify your bundle identifier 'xxx' is correct.
1.错误提示 打包工程,Validate 的时候提示: No suitable application records were found. Verify your bundle identifie ...