Azure Flask 部署 - WSGI 接口 [英] Azure Flask Deployment - WSGI Interface

查看:22
本文介绍了Azure Flask 部署 - WSGI 接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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)

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

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)

当我尝试在 Azure 模拟器中运行它时收到以下错误.

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

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

我怀疑我没有将 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.

任何帮助将不胜感激.

推荐答案

经过一些麻烦的拍摄后,我找到了问题的解决方案,但遗憾的是无法准确找出问题所在.

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.

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

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:

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

  • 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()

这也可能有所帮助.

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

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

这篇关于Azure Flask 部署 - WSGI 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆