首页 > 解决方案 > 未找到 Azure Flask 路由

问题描述

我正在使用 Visual Studio 创建一个空白的 Flask 应用程序。当我在本地运行应用程序时,我得到了预期的“hello world”。当我发布到 Azure 应用服务时,我得到了这个漂亮丑陋的蓝色主页,这不是我制作的。我的项目中不存在此代码,至少我可以在 Visual Studio 解决方案资源管理器中看到。如果我尝试导航到<my account name>.azurewebsites.net/test我得到一个404错误。有什么建议吗?

在此处输入图像描述

应用程序.py:

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('/test')
@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)

网络部署.pubxml:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <ResourceId>/subscriptions/78439574-998e-4216-b54c-40fa45d65ca5/resourceGroups/Default-SQL-EastUS/providers/Microsoft.Web/sites/FlaskWebProject220180702122411</ResourceId>
    <ResourceGroup>Default-SQL-EastUS</ResourceGroup>
    <PublishProvider>AzureWebSite</PublishProvider>
    <LastUsedBuildConfiguration>Debug</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish>http://flaskwebproject220180702122411.azurewebsites.net</SiteUrlToLaunchAfterPublish>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <MSDeployServiceURL>flaskwebproject220180702122411.scm.azurewebsites.net:443</MSDeployServiceURL>
    <DeployIisAppPath>FlaskWebProject220180702122411</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
    <EnableMSDeployBackup>True</EnableMSDeployBackup>
    <UserName>$FlaskWebProject220180702122411</UserName>
    <_SavePWD>True</_SavePWD>
    <_DestinationType>AzureWebSite</_DestinationType>
  </PropertyGroup>
</Project>

更新:

我按照 Jay 列出的步骤进行操作。我现在收到“页面无法显示,因为发生了内部服务器错误。”。我在应用程序设置中添加了一个日志文件,但是它没有出现。

在此处输入图像描述

在此处输入图像描述

更新 2

我注意到在我的 web.config 中版本不同,所以我更新了它。这允许创建 logs.txt。

  <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python364x64\python.exe|D:\home\Python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>

我仍然收到 500 错误。

日志.txt: 在此处输入图像描述

标签: pythonazureflaskdeployment

解决方案


请参考我的工作步骤,看看错误是否仍然出现。

第 1 步:创建 azure web 应用并添加扩展(这里是 Python 3.6.1 x64)

在此处输入图像描述

第 2 步:发布您的flask项目并添加web.config.

网络配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="WSGI_HANDLER" value="<Your Project Name>.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\Python361x64\python.exe|D:\home\Python361x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
    </handlers>
  </system.webServer>
</configuration>

如果部署成功,您可以在KUDU路径中看到以下结构:D:\home\site\wwwroot>.

在此处输入图像描述

如果你想使用额外的python包,请继续。

第三步:切换到 Kudu CMD 和命令cd Python361x64touch get-pip.py并将 url 的内容复制https://bootstrap.pypa.io/get-pip.pyget-pip.pyvia Edit 按钮,然后运行python get-pip.py安装 pip 工具。

在此处输入图像描述

第 4 步:通过以下方式安装您需要的任何软件包python -m pip install pyodbc

在此处输入图像描述

希望它可以帮助你。有任何疑问,请告诉我。


更新

在我的步骤中,复制内容https://bootstrap.pypa.io/get-pip.py以安装 pip。它可以识别系统python版本,安装相应的python包。

在此处输入图像描述

所以,你只需要运行命令python -m pip install newspaper3k来安装newspaper3k.

在此处输入图像描述


推荐阅读