【问题标题】:WSGI error migrating Python/Flask app from Heroku to Azure将 Python/Flask 应用程序从 Heroku 迁移到 Azure 时出现 WSGI 错误
【发布时间】:2018-06-18 16:38:19
【问题描述】:

我在将小型 Flask 应用程序部署到 Azure 时遇到了困难。该应用程序在本地和 Heroku 上运行良好,但在 Azure 上返回内部服务器错误。这是日志:

logs.txt

StdErr: 
2018-06-15 22:14:38.239395: Unhandled exception in wfastcgi.py: Traceback (most recent call last):
  File "D:\home\python353x86\wfastcgi.py", line 791, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "D:\home\python353x86\wfastcgi.py", line 633, in read_wsgi_handler
    handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
  File "D:\home\python353x86\wfastcgi.py", line 603, in get_wsgi_handler
    handler = getattr(handler, name)
AttributeError: module 'app' has no attribute 'wsgi_app'
2018-06-15 22:14:38.255034: Running on_exit tasks
2018-06-15 22:14:38.270645: wfastcgi.py 3.0.0 closed

这似乎是 WSGI 配置的问题。这是我的第一次 Azure 部署,以前我从来没有过多考虑过 WSGI。根据教程,我在 Heroku 上使用 Green Unicorn,在 requirements.txtProcfile 中引用了它:

过程文件

web: gunicorn app:app

requirements.txt

Flask==0.12.2
Flask-Cors==3.0.3
gunicorn==19.7.1

但是 Azure 不使用 Procfile,我不清楚 gunicorn 是否可以在 Azure 环境中工作。

我的问题是:

  1. 绿色独角兽可行吗?如果是,我该如何配置?
  2. 如果不是,我还有哪些其他选择?根据this 之类的帖子,我尝试在app 声明下方添加wsgi_app = app.wsgi_app 行,但这会返回一个错误,导致我陷入另一个兔子洞:TypeError: wsgi_app() missing 2 required positional arguments: 'environ' and 'start_response'

这是后端的其余部分:

app.py

# Dependencies ------------------------
from flask import Flask
from flask_cors import CORS, cross_origin
from flask import render_template
from flask import request
import sqlite3
import json
import os

# Config ------------------------------
app = Flask(__name__)
CORS(app)

# Routes ------------------------------
@app.route("/")
def home():
    # Do stuff

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="app.wsgi_app()" />
    <add key="PYTHONPATH" value="D:\home\site\wwwroot" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
  <add name="Python FastCGI" path="handler.fcgi" verb="*"     modules="FastCgiModule" 
      scriptProcessor="D:\home\python353x86\python.exe|    D:\home\python353x86\wfastcgi.py" 
          resourceType="Unspecified" requireAccess="Script" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
        <add input="{REQUEST_URI}" pattern="^/static/.*"     ignoreCase="true" negate="true" />
          </conditions>
      <action type="Rewrite" url="handler.fcgi/{R:1}"     appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

在这一点上,我只是盲目地复制/粘贴可能的解决方案,所以我非常感谢任何帮助。谢谢。

【问题讨论】:

    标签: python azure flask gunicorn wsgi


    【解决方案1】:

    尝试定义 wsgi_app:

    app = Flask(__name__)
    
    # define for IIS module registration.
    wsgi_app = app.wsgi_app
    
    if __name__ == '__main__':
        app.run()
    

    【讨论】:

    • 同样的 500 错误。这是日志:TypeError: wsgi_app() missing 2 required positional arguments: 'environ' and 'start_response'
    • 尝试将&lt;add key="WSGI_HANDLER" value="my_app.wsgi_app()" /&gt; 更改为&lt;add key="WSGI_HANDLER" value="my_app.wsgi_app" /&gt;
    • 做到了。谢谢!
    • @GeorgeSibble 我只有一个 py 文件,没有 web.config 文件,因为它是一个简单的 API。我必须创建 web.config 以避免 2 个缺少参数错误吗?
    猜你喜欢
    • 2018-11-23
    • 2016-10-03
    • 2020-03-15
    • 2023-03-09
    • 2020-05-24
    • 2018-04-07
    • 2022-01-05
    • 2016-05-15
    相关资源
    最近更新 更多