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.pswp.cn/news/389395.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389395.shtml
英文地址,請注明出處:http://en.pswp.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; 測試是使用人工操作或者程序自動運行的方式來檢驗它是否滿足規定的需求或弄清預期結果與實際結果之間的差別的過程。 它…