Configuring python for IIS

0

Following the steps in https://docs.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2019 to configure flask app to run behind IIS and searching online I couldn't find a solution that fix my problem.

I have my web.config as:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="PythonHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
    </handlers>
    <httpPlatform processPath="C:\envs\Scripts\python.exe"
                  arguments="-m flask run --port %HTTP_PLATFORM_PORT%"
                  stdoutLogEnabled="true"
                  stdoutLogFile="C:\logs\python.log"
                  startupTimeLimit="60"
                  processesPerApplication="16">
      <environmentVariables>
        <environmentVariable name="FLASK_APP" value="app.py" />
      </environmentVariables>
    </httpPlatform>
  </system.webServer>
</configuration>

A visit to the site just spit the following. I tried all possible solutions I came across which include giving IIS_IUSRS access. Running the app on command prompt runs just fine. The IIS error message is not helping.

enter image description here

edit:

after installing the http platformhandler and now on a different dev box, I can see the handler at work but just a different monster: 502.3 Bad Gateway

Detailed Error Information:

Module httpPlatformHandler Requested URL http://127.0.0.1:5007/about

Notification ExecuteRequestHandler Physical Path C:\inetpub\wwwroot\app\about

Handler PythonHandler Logon Method Anonymous

Error Code 0x8007042b Logon User Anonymous

Which tells me the handler is just handling the url as just folders in my app root because http://127.0.0.1:5007/about is nothing but a route to:

myapp_about.py:

from flask import Blueprint, jsonify

myapp_about = Blueprint('about', __name__)


@myapp_about.route('/about')
def get_about():
    return jsonify({"wow": "We really are routed to here. maybe not"})
python
iis
flask
asked on Stack Overflow Apr 11, 2019 by waitforit • edited Apr 12, 2019 by waitforit

3 Answers

0

I didn't see anywhere in those instructions, but I think your problem is the HttpPlatformHandler. Try removing that and see if you get a better/different error. HttpPlatformHandler is not a default configuration element of IIS and need to be added, most likely by installing something like https://www.iis.net/downloads/microsoft/httpplatformhandler.

answered on Stack Overflow Apr 11, 2019 by Rich-Lang
0

Had same issue and worked doing below. "Took a lot of effort but I got it to work a last

Must Install HttpPlatform https://www.iis.net/downloads/microsoft/httpplatformhandler Configuration Editor system.webServer/httpPlatform (Section is not locked) Make sure app has permissions for log path stdoutLogFile="c:\home\LogFiles\python.log" Had to setup up app.run(port=PORT) PORT = int(environ.get('SERVER_PORT', '5555')) Setup firewall to allow http-incoming requests"

answered on Stack Overflow Jan 21, 2020 by Chip Humphrey
0

This worked for me on Windows 10 and Windows Server 2019, both using IIS 10.0. You must have the HttpPlatformHandler installed, as mentioned in the other answers. (Download it via Windows Platform Installer.)

The <handlers> section of my web.config is locked, so instead of editing the web.config directly, I used the Add Module Mapping... action on the IIS Handler Mappings GUI to add the Python handler:

Image of Python handler

You can also edit the website configuration directly via the IIS Manager's GUI:

IIS Configuration Editor

I use an absolute path for the FLASK_APP environment variable, but it seems to work without it too. (In fact, you can leave the environment variable out completely if you are using the default Flask app name of app.py.)

web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpPlatform processPath="C:\Python37\python.exe"
                      arguments="-m flask run --port %HTTP_PLATFORM_PORT%"
                      stdoutLogEnabled="true"
                      stdoutLogFile="C:\inetpub\logs\LogFiles\python.log"
                      startupTimeLimit="60"
                      processesPerApplication="16">
          <environmentVariables>
              <environmentVariable name="FLASK_APP" value="C:\inetpub\wwwroot\hello.py" />
          </environmentVariables>
        </httpPlatform>
    </system.webServer>
</configuration>

This works with the minimal Flask app below. (Maybe try this before adding Blueprint and different port numbers.)

hello.py:

from flask import Flask

app = Flask(__name__)

@app.route('/hi')
def hello_world():
    return 'Hello, World!'

Image of browser output

Notes about Windows Security permissions:

  • It is not necessary for the Handler Mapping in IIS Manager to have Execute permissions checked. (Script permission is the default, and works as-is.)
  • It is not necessary for IIS_IUSR to have execute permissions on python.exe or flask.exe.
  • It is not necessary for IIS_IUSR to have execute permissions on app.py or hello.py.
  • If logging is enabled, IIS_IUSR must have write permissions on the target folder (e.g. C:\inetpub\logs\LogFiles) - not just on the python.log file, since it will likely add an additional suffix to the specified log file name.
answered on Stack Overflow Jul 29, 2020 by Dirco • edited Oct 27, 2020 by Dirco

User contributions licensed under CC BY-SA 3.0