python - 未找到 Azure Flask 路由

标签 python azure flask deployment

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

enter image description here

app.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)

web deploy.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 列出的步骤进行操作。我现在收到一条“由于发生内部服务器错误,无法显示该页面。”。我在应用程序设置中添加了日志文件,但是它没有出现。

enter image description here

enter image description here

更新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: enter image description here

最佳答案

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

第 1 步:创建 Azure Web 应用程序并添加扩展(此处为 Python 3.6.1 x64)

enter image description here

第 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>

enter image description here

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

第3步:切换到Kudu CMD并命令cd Python361x64touch get-pip.py并复制url内容https://通过编辑按钮将 bootstrap.pypa.io/get-pip.py 复制到 get-pip.py 中,然后运行 ​​python get-pip.py 进行安装点工具。

enter image description here

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

enter image description here

希望对您有帮助。如有任何疑问,请告诉我。


更新:

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

enter image description here

因此,您只需运行命令python -m pip installnewspaper3k即可安装newspaper3k

enter image description here

关于python - 未找到 Azure Flask 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51143378/

相关文章:

python - 保护Python代码,包括密码、加密?

Azure DataCache MaxConnectionToServer

Azure 数据磁盘 - 从导出的磁盘文件获取文件

python - 当我渲染模板时,如何在 Flask 中添加缓存控制?

python - Flask 设置代码运行一次,但不在测试期间运行

Python Flask-Restful 错误 : Delete method in API is deleting ALL database entries

python - 如何为 ruamel.yaml 创建自定义 yaml 映射转储程序?

python - Heroku procfile 多个 worker

python - 如何获取值 ('content_object' )?

azure - 从不同组织访问 Azure DevOps Artifact Feed