当前位置: 首页 > 面试题库 >

Azure Flask部署-WSGI界面

南门飞
2023-03-14
问题内容

我目前正在阅读《 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变量放置在正确的位置,但是我无法弄清楚应该将其放置在什么位置。

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


问题答案:

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

基本上,我经历了在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()
    

这可能也有帮助。

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



 类似资料:
  • WSGI 没有官方的实现, 因为WSGI更像一个协议. 只要遵照这些协议,WSGI应用(Application )都可以在任何实现(Server)上运行, 反之亦然 WSGI:Python Web Server Gateway Interface v1.0 它是 PEP333 中定义的(PEP333的目标建立一个简单的普遍适用的服务器与Web框架之间的接口) WSGI是Python应用程序或框架和

  • The following classes and functions are designed to make working with the WSGI specification easier or operate on the WSGI layer. All the functionality from this module is available on the high-level

  • A Serverless Framework plugin to build your deploy Python WSGI applications using Serverless. CompatibleWSGI application frameworks include Flask, Django and Pyramid - for a complete list, see:http://

  • 我们终于准备好向全世界展示我们的应用程序了。是时候要部署。这个过程可能是痛苦的因为有许多琐碎的事情要去做。当涉及到生产环境的搭建以及服务器的配置方案,这是有很多的选择需要做出。在本章中,我们会讨论一些重要的部分以及一些我们可能会用到的选项(关于主机或者服务器的搭建方式等等)。 主机 首先我们需要一台服务器。现在有成千上万的服务器供应商,但是我个人建议的有三家。我不打算在这里介绍如何开始使用它们的细

  • 默认方案 umi@2 默认对新手友好,所以默认不做按需加载处理,umi build 后输出 index.html、umi.js 和 umi.css 三个文件。 不输出 html 文件 某些场景 html 文件交给后端输出,前端构建并不需要输出 html 文件,可配置环境变量 HTML=none 实现。 $ HTML=none umi build 部署 html 到非根目录 经常有同学问这个问题:

  • 3.6 部署 本节介绍如何部署Nginx配置文件。 3.6.1 Nginx配置文件组成方式 我们是这样管理Nginx文件的: 在Nginx服务器上,nginx/conf目录下,建立了一个phoenix-slb目录。该目录下存放着由Camel管理的站点配置文件。 每个站点对应着一个单独的文件夹,文件夹下有一个server.conf,存放着本站点的配置。 假设有两个站点a.liyang.com,b