Azure Flask部署- WSGI接口



我目前正在阅读《Flask Web Development, Developing Web Applications with Python》这本书,并且目前在决定我应该把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/

它会自动为你设置一个网站,并处理网络。

我用Flask构建了一个测试Azure云服务,试图重现您的问题,幸运的是,我发现了问题。

我将相关包复制到我的测试项目中,发现如果根目录下的入口文件命名为 app.py ,也会出现和你一样的错误。但是我把文件重命名为 manage.py ,项目工作得很好。

根据我的理解,也许入口文件app.py和名为app的包在映射时会发生冲突。

经过一些麻烦的拍摄后,我能够找到解决问题的方法,但不幸的是无法准确地找出问题所在。

基本上,我经历了在VS2015中从头开始重建测试项目的过程(Python -> Azure云服务-> Flask Web Role),并且不知何故,这次能够使用7a测试项目获得一个工作解决方案,它在Azure模拟器中运行,随后成功将其发布为Azure Web应用程序。

我认为我的问题可能是由下列问题之一引起的:

    requirements.txt文件很可能不是最新的。请注意,当你运行Azure模拟器时,它会检查requirements.txt文件,并自动更新/安装你当前没有安装在python环境中的任何库(没有提示)。我可能没有ConfigureCloudService。在Flask Worker Role Project的bin文件夹下,找到ps1或ps1 .cmd文件。(自述也值得一读。
  • 我还将manage.py文件的基础更改为:

    if __name__ == '__main__':
        app.run()
    

这可能也有帮助。

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

最新更新