python - 未找到 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 错误。
解决方案
请参考我的工作步骤,看看错误是否仍然出现。
第 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 Python361x64
,touch get-pip.py
并将 url 的内容复制https://bootstrap.pypa.io/get-pip.py
到get-pip.py
via 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
.
推荐阅读
- google-chrome - 如何在 chrome 上自动播放音频
- javascript - 如何在考虑俯仰和方位的情况下获取 mapbox-gl 地图视口边缘坐标?
- c# - 如何使用 restsharp 获取 magento 管理员令牌
- c# - ZeroMQ (NetMQ) TCP 传输可以在同一进程中的发布者和订阅者之间使用吗?
- python - python识别电子邮件中的文本
- .net - 无需登录的基于角色的授权
- c++ - 如何检查已安装的字体样式?
- jenkins - Jenkins-Groovy - Jfrog Artifactory - 下载工件时出现异常计算的 MD5 校验和与原始校验和不同
- c++ - 如何在 V4 打印机驱动程序中设置输出文件的修复目录
- react-native - React Native:以不同的分辨率测试应用程序