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 Managerapp = Flask(__name__)
# configure your appmanager = 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 Commandclass 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 worldshell               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 付勇’

转载于:https://www.cnblogs.com/jiangchunsheng/p/9218340.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389395.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

上采样(放大图像)和下采样(缩小图像)(最邻近插值和双线性插值的理解和实现)

上采样和下采样 什么是上采样和下采样? • 缩小图像(或称为下采样(subsampled)或降采样(downsampled))的主要目的有 两个:1、使得图像符合显示区域的大小;2、生成对应图…

r语言绘制雷达图_用r绘制雷达蜘蛛图

r语言绘制雷达图I’ve tried several different types of NBA analytical articles within my readership who are a group of true fans of basketball. I found that the most popular articles are not those with state-of-the-art machine learning technologies, but tho…

java 分裂数字_分裂的补充:超越数字,打印物理可视化

java 分裂数字As noted in my earlier Nightingale writings, color harmony is the process of choosing colors on a Color Wheel that work well together in the composition of an image. Today, I will step further into color theory by discussing the Split Compleme…

Java 集合 之 Vector

http://www.verejava.com/?id17159974203844 import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Vector;public class Test {/*** param args the command line arguments*/public static void main(String[] args) {//打印…

前端电子书单大分享~~~

前言 纯福利, 如果你不想买很多书,只想省钱看电子书; 如果你找不到很多想看书籍的电子书版本; 那么,请保存或者下载到自己的电脑或者手机或者网盘吧。 不要太着急,连接在最后呢 前端 前端框架 node html-cs…

结构化数据建模——titanic数据集的模型建立和训练(Pytorch版)

本文参考《20天吃透Pytorch》来实现titanic数据集的模型建立和训练 在书中理论的同时加入自己的理解。 一,准备数据 数据加载 titanic数据集的目标是根据乘客信息预测他们在Titanic号撞击冰山沉没后能否生存。 结构化数据一般会使用Pandas中的DataFrame进行预处理…

比赛,幸福度_幸福与生活满意度

比赛,幸福度What is the purpose of life? Is that to be happy? Why people go through all the pain and hardship? Is it to achieve happiness in some way?人生的目的是什么? 那是幸福吗? 人们为什么要经历所有的痛苦和磨难? 是通过…

带有postgres和jupyter笔记本的Titanic数据集

PostgreSQL is a powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.PostgreSQL是一个功能强大的开源对象关系数据库系统&am…

Django学习--数据库同步操作技巧

同步数据库:使用上述两条命令同步数据库1.认识migrations目录:migrations目录作用:用来存放通过makemigrations命令生成的数据库脚本,里面的生成的脚本不要轻易修改。要正常的使用数据库同步的功能,app目录下必须要有m…

《20天吃透Pytorch》Pytorch自动微分机制学习

自动微分机制 Pytorch一般通过反向传播 backward 方法 实现这种求梯度计算。该方法求得的梯度将存在对应自变量张量的grad属性下。 除此之外,也能够调用torch.autograd.grad 函数来实现求梯度计算。 这就是Pytorch的自动微分机制。 一,利用backward方…

React 新 Context API 在前端状态管理的实践

2019独角兽企业重金招聘Python工程师标准>>> 本文转载至:今日头条技术博客 众所周知,React的单向数据流模式导致状态只能一级一级的由父组件传递到子组件,在大中型应用中较为繁琐不好管理,通常我们需要使用Redux来帮助…

机器学习模型 非线性模型_机器学习模型说明

机器学习模型 非线性模型A Case Study of Shap and pdp using Diabetes dataset使用糖尿病数据集对Shap和pdp进行案例研究 Explaining Machine Learning Models has always been a difficult concept to comprehend in which model results and performance stay black box (h…

5分钟内完成胸部CT扫描机器学习

This post provides an overview of chest CT scan machine learning organized by clinical goal, data representation, task, and model.这篇文章按临床目标,数据表示,任务和模型组织了胸部CT扫描机器学习的概述。 A chest CT scan is a grayscale 3…

Pytorch高阶API示范——线性回归模型

本文与《20天吃透Pytorch》有所不同,《20天吃透Pytorch》中是继承之前的模型进行拟合,本文是单独建立网络进行拟合。 代码实现: import torch import numpy as np import matplotlib.pyplot as plt import pandas as pd from torch import …

vue 上传图片限制大小和格式

<div class"upload-box clear"><span class"fl">上传图片</span><div class"artistDet-logo-box fl"><el-upload :action"this.baseServerUrl/fileUpload/uploadPic?filepathartwork" list-type"pic…

作业要求 20181023-3 每周例行报告

本周要求参见&#xff1a;https://edu.cnblogs.com/campus/nenu/2018fall/homework/2282 1、本周PSP 总计&#xff1a;927min 2、本周进度条 代码行数 博文字数 用到的软件工程知识点 217 757 PSP、版本控制 3、累积进度图 &#xff08;1&#xff09;累积代码折线图 &…

算命数据_未来的数据科学家或算命精神向导

算命数据Real Estate Sale Prices, Regression, and Classification: Data Science is the Future of Fortune Telling房地产销售价格&#xff0c;回归和分类&#xff1a;数据科学是算命的未来 As we all know, I am unusually blessed with totally-real psychic abilities.众…

openai-gpt_为什么到处都看到GPT-3?

openai-gptDisclaimer: My opinions are informed by my experience maintaining Cortex, an open source platform for machine learning engineering.免责声明&#xff1a;我的看法是基于我维护 机器学习工程的开源平台 Cortex的 经验而 得出 的。 If you frequent any part…

Pytorch高阶API示范——DNN二分类模型

代码部分&#xff1a; import numpy as np import pandas as pd from matplotlib import pyplot as plt import torch from torch import nn import torch.nn.functional as F from torch.utils.data import Dataset,DataLoader,TensorDataset""" 准备数据 &qu…

OO期末总结

$0 写在前面 善始善终&#xff0c;临近期末&#xff0c;为一学期的收获和努力画一个圆满的句号。 $1 测试与正确性论证的比较 $1-0 什么是测试&#xff1f; 测试是使用人工操作或者程序自动运行的方式来检验它是否满足规定的需求或弄清预期结果与实际结果之间的差别的过程。 它…