Azure Flask Routes Not found

2019-07-14 00:20发布

I am using visual studio to create a blank Flask app. When I run the application locally, I get the expected "hello world". When I publish to Azure App Service, I get this nice ugly blue home page that I didn't make. No where in my project does this code exist, atleast that I can see in visual studio solution explorer. If I try to nagivate to <my account name>.azurewebsites.net/test I get a 404 error. Any suggestion?

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>

UPDATE:

I followed the steps listed by Jay. I am now getting a "The page cannot be displayed because an internal server error has occurred.". I added a log file in the application setting, however, it does not appear.

enter image description here

enter image description here

UPDAtE 2

I noticed that in my from web.config the versions were different so i updated it. this allowed the logs.txt to be created.

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

I am still getting a 500 error.

logs.txt: enter image description here

1条回答
够拽才男人
2楼-- · 2019-07-14 01:16

Please refer to my work steps and see if the error still shows up.

Step 1 : Create azure web app and add Extensions(here is Python 3.6.1 x64)

enter image description here

Step 2 : Publish your flask project and add the web.config.

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>

If you deploy successfully, you could see following structure in the KUDU path: D:\home\site\wwwroot>.

enter image description here

If you want to use additional python package, please go on.

Step 3: Switch to the Kudu CMD and commands cd Python361x64 and touch get-pip.py and copy the content of the url https://bootstrap.pypa.io/get-pip.py into the get-pip.py via Edit button, then run python get-pip.py to install the pip tool.

enter image description here

Step 4 : Install any packages you need via python -m pip install pyodbc

enter image description here

Hope it helps you. Any concern ,please let me know.


Update:

In my steps, copy content in https://bootstrap.pypa.io/get-pip.py to install pip. It could identify the system python version, install the corresponding python package.

enter image description here

So,you just need to run the command python -m pip install newspaper3k to install newspaper3k.

enter image description here

查看更多
登录 后发表回答