windows与linux启动flask项目demo区别

6 篇文章 0 订阅

windows 下

目录结构

在这里插入图片描述

home.py
#import os
#import json
#import re
#import pandas as pd
#import unicodedata
#from datetime import datetime
#from pathlib import Path
#from flaskr.logger import logging
#from werkzeug.utils import secure_filename
from flask import (
    Blueprint, flash, g, redirect, render_template, request, Response, session, url_for, jsonify, send_file
)

bp = Blueprint('home', __name__, url_prefix='/MQ')


@bp.route('heartbeat', methods=('GET', "POST"))
def heartbeat():
    return "alive"

main.py
import os

from flask import Flask
from flask_cors import CORS

def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    CORS(app, supports_credentials=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
        MAX_CONTENT_LENGTH= 500 * 1024 * 1024
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    # a simple page that says hello
    import home
    app.register_blueprint(home.bp)
    #app.register.blueprint(run.bp)
    return app

app = create_app()

if __name__ == '__main__':

    app.run(host='0.0.0.0' , port= '9999', debug=True)

init.py (非必要)

运行方式

python main.py

在这里插入图片描述

访问测试

在这里插入图片描述

linux 下

home.py 内容相同,main.py 去掉

init.py
import os

from flask import Flask
from flask_cors import CORS

def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    CORS(app, supports_credentials=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
        MAX_CONTENT_LENGTH= 500 * 1024 * 1024
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    # a simple page that says hello
    import home
    app.register_blueprint(home.bp)
    #app.register.blueprint(run.bp)
    return app

运行方式

## 开发模式
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run -h 0.0.0.0 -p 9999

在这里插入图片描述

## 生产模式

export FLASK_APP=flaskr
nohup gunicorn -t 300 -w 1 -b 0.0.0.0:9999 'flaskr:create_app()'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值