小编典典

Azure Flask部署-WSGI界面

python

我目前正在阅读《 FlaskWeb开发,用Python开发Web应用程序》一书,并且在确定应放置WSGI接口的位置时遇到一些问题,以便可以将其部署到Azure WebService。作为参考,

为了尝试解决问题出在哪里,我在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变量放置在正确的位置,但是我无法弄清楚应该将其放置在什么位置。

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


阅读 244

收藏
2021-01-20

共1个答案

小编典典

经过一番令人不安的拍摄后,我能够找到解决问题的方法,但不幸的是,无法确切地找出问题所在。

基本上,我经历了在VS2015中从头开始重建测试项目的过程(Python-> Azure Cloud Service-> Flask Web
Role),并且这次可以使用7a测试项目并在Azure中运行该解决方案来获得可行的解决方案模拟器,然后将其成功发布为Azure Web App。

我相信我的问题可能是由以下问题之一引起的:

  • requirements.txt文件很可能不是最新的。请注意,当您运行Azure模拟器时,它将检查requirements.txt文件并自动更新/安装您当前未在python环境中安装的任何库(无提示)。
  • Flask Worker Role Project的bin文件夹中可能没有ConfigureCloudService.ps1或ps.cmd文件。(如果遇到任何问题,也值得阅读Readme.mht文件)
  • 我还将manage.py文件的基础更改为:
    if __name__ == '__main__':
    app.run()
    

这可能也有帮助。

我希望这可以帮助其他可能遇到类似问题的人。

2021-01-20