编程语言
首页 > 编程语言> > python-Azure Flask部署-WSGI界面

python-Azure Flask部署-WSGI界面

作者:互联网

我目前正在阅读《 Flask Web开发,用Python开发Web应用程序》一书,并且在确定应放置WSGI接口的位置时遇到一些问题,以便可以将其部署到Azure Web Service.供参考,我目前在第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)

此处的关键行是wfastcgi选择的wsgi_app属性的声明.但是,当我尝试将其插入以下代码(供参考的代码为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变量放置在正确的位置,但是我无法弄清楚应该在哪里放置它.

任何帮助将是极大的赞赏.

解决方法:

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

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

标签:azure,visual-studio-2015,flask,ptvs,python
来源: https://codeday.me/bug/20191027/1948112.html