将Flask应用程序部署到Azure

时间:2018-06-14 05:14:38

标签: azure flask azure-web-sites wsgi

使用Flask部署Azure Web时,我一直收到以下错误:

Unhandled exception in wfastcgi.py: Traceback (most recent call last):
  File "D:\home\python364x64\wfastcgi.py", line 791, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:\home\python364x64\wfastcgi.py", line 633, in read_wsgi_handler
    handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
  File "D:\home\python364x64\wfastcgi.py", line 603, in get_wsgi_handler
    handler = getattr(handler, name)
AttributeError: module 'app' has no attribute 'app'

这是我的应用程序的结构:

|-app
    |- __init__.py
    |- mod1
    |- mod2
|-config.py
|-runserver.py
|-web.config

这是我的web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="app.app"/>
    <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
    <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
  </appSettings>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\python364x64\python.exe|D:\home\python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

config.py:

import os

app_dir = os.path.abspath(os.path.dirname(__file__))

class BaseConfig:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'A SECRET KEY'

class DevelopementConfig(BaseConfig):
    DEBUG = True

runserver.py:

import os
from app import db, create_app

app = create_app(os.getenv('FLASK_ENV') or 'config.DevelopementConfig')


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

和app。 init .py:

from flask import Flask
def create_app(config):
    app = Flask(__name__)
    app.config.from_object(config)
    return app

我尝试将wsgi.py包含在:

from app import app as application

与runserver.py处于同一级别,但仍然出现上述错误。我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

那么,WSGI如何运作?它启动,导入您的应用程序并调用它的方法来处理请求。 WSGI的<add key="WSGI_HANDLER" value="app.app"/>表示导入模块应用并从中获取变量应用。但在导入app.__init__.py之后,只有Flaskcreate_app变量,具有Flask-class和create_app-function。所以你应该在WSGI_HANDLER中设置指向flask应用程序的值,runserver.appapp.create_app()(我不知道究竟是什么,使用正确的语法,Azure将app作为一个函数处理,但它{{3} })。