Azure Flask Deployment - WSGI Interface

我怕爱的太早我们不能终老 提交于 2019-12-18 07:04:09

问题


I'm currently working through the book Flask Web Development, Developing Web Applications with Python and am currently having some issues determining where I should place the WSGI interface so that I can deploy it to an Azure Web Service. For reference I'm currently at Chapter 7 and a copy of this code that I'm currently working through can be found at https://github.com/miguelgrinberg/flasky/tree/7a

To try and work out where the problem is I've created a test Azure Cloud Service with Flask in Visual Studio which runs perfectly in the Azure Emulator. The following code is a copy of the app.py file.

"""
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)

The key line here is the declaration of the wsgi_app attribute which is picked up by wfastcgi. However when I try to insert this into the following code (manage.py for reference) and change it slightly to run with the test project settings

#!/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)

I receive the following error when I try to run it inside of an Azure Emulator.

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

I suspect that I'm not putting the wsgi_app variable in the correct location but I can't figure out exactly where I should put it.

Any help would be greatly appreciative.


回答1:


Have you considered using a web app to get Flask up and running? Here is a comprehensive guide about how to deploy Flask on a web app: https://azure.microsoft.com/en-us/documentation/articles/web-sites-python-create-deploy-flask-app/

It will automatically set you up with a site and handle the web.config and fast cgi scripts.




回答2:


I built a test Azure Cloud Service with Flask, tried to reproduce your issue, fortunately, I found the problem.

I copied relative packages to my test project, the found if the entrance file in root directory was named app.py, it would occur the same error with you. But I renamed the file to manage.py the project worked fine.

Per my understanding, maybe the entrance file app.py and the package named app would be in conflict for mapping.




回答3:


After a bit of troubling shooting I was able to find a solution to my problem but was unfortunately unable to isolate exactly what went wrong.

Basically I went through the process of rebuilding my test project from scratch in VS2015 (Python -> Azure Cloud Service -> Flask Web Role) and was somehow this time able to get a working solution using the 7a test project with it running in the Azure Emulator followed by successfully publishing it as an Azure Web App.

I believe my problem could have resulted from one of the following issues:

  • The requirements.txt file was most likely not up to date. Note that when you run the Azure Simulator it checks the requirements.txt file and updates/installs any libraries that you don't currently have installed in your python environment automatically (without a prompt).
  • I possibly didn't have the ConfigureCloudService.ps1 or ps.cmd file in the bin folder in the Flask Worker Role Project. (It's also worthwhile reading through the Readme.mht file if you run into any problems)
  • I also changed the base of the manage.py file to:

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

Which may have helped as well.

I hope this helps out anyone else who may run into a similar issue.



来源:https://stackoverflow.com/questions/32518358/azure-flask-deployment-wsgi-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!