python - Azure Flask 部署 - WSGI 接口(interface)

标签 python azure flask visual-studio-2015 ptvs

我目前正在阅读《Flask Web 开发,使用 Python 开发 Web 应用程序》一书,并且目前在确定应该将 WSGI 接口(interface)放置在何处以便可以将其部署到 Azure Web 服务时遇到一些问题。作为引用,我目前正在阅读第 7 章,我当前正在处理的此代码的副本可以在 https://github.com/miguelgrinberg/flasky/tree/7a 中找到。

为了尝试找出问题所在,我在 Visual Studio 中使用 Flask 创建了一个测试 Azure 云服务,该服务在 Azure 模拟器中完美运行。以下代码是 app.py 文件的副本。

"""
This script runs the application using a development server.
It contains the definition of routes and views for the application.
"""

from flask import Flask
app = Flask(__name__)

# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app

@app.route('/')
def hello():
    """Renders a sample page."""
    return "Hello World!"

if __name__ == '__main__':
    import os
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555
    app.run(HOST, PORT)

这里的关键行是 wsgi_app 属性的声明,该属性由 wfastcgi 选取。但是,当我尝试将其插入以下代码(manage.py 供引用)并稍微更改它以使用测试项目设置运行时

#!/usr/bin/env python
import os
from app import create_app, db
from app.models import User, Role
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)


@manager.command
def test():
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)

# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app

if __name__ == '__main__':
    HOST = os.environ.get('SERVER_HOST', 'localhost')
    try:
        PORT = int(os.environ.get('SERVER_PORT', '5555'))
    except ValueError:
        PORT = 5555
    app.run(HOST, PORT)

当我尝试在 Azure 模拟器内运行它时,收到以下错误。

AttributeError: 'module' object has no attribute 'wsgi_app'

我怀疑我没有将 wsgi_app 变量放在正确的位置,但我无法弄清楚应该将它放在哪里。

任何帮助将不胜感激。

最佳答案

您是否考虑过使用网络应用来启动并运行 Flask?以下是有关如何在 Web 应用程序上部署 Flask 的综合指南: https://azure.microsoft.com/en-us/documentation/articles/web-sites-python-create-deploy-flask-app/

它将自动为您设置一个站点并处理 web.config 和快速 cgi 脚本。

关于python - Azure Flask 部署 - WSGI 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32518358/

相关文章:

python - 有没有好的Python库可以生成和渲染图像格式的文本?

json - 使用 test_request_context (Flask) 中传递的无效 JSON 进行测试

python - 如何使用 Python Selenium 在文本框(输入)中定位和插入值?

Python HTTP Server - 在不使用 HTTP 模块的情况下创建

javascript - react |网页包 |在 Azure 上获取 process.env.NODE_ENV== 未定义

azure - 如何将 azure 磁盘恢复到以前的快照?

flask - '没有找到应用程序。要么在 View 函数内工作,要么推送'RuntimeError : No application found

python - imutils VideoStream与flask集成时返回NoneType

Python IDLE 相当于 R 中的 CTRL-R

azure 而不是 LA(MP)