Python Flask WebAPI - chanandrew96/MyLearning GitHub Wiki

Flask

Start flask application

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

Execute the flask python file:
flask --app [python filename] run

Host Flask on IIS

Windows Feature

  • Open "Turn Windows Feature on or off" and enable "CGI' under "IIS/WWW Services/Application Development Features"

Install & enable wfastcgi

  • Execute pip install wfastcgi to install wfastcgi
  • Go to your Python\Scripts folder
    You may find the Python folder location by where python
    Then execute the wfastcgi-enable.exe
    Or you may execute wfastcgi wfastcgi-enable to enable wfastcgi

IIS Permission Setting

Add Permission for IUSR in order for IIS to access your application folder

icacls . /grant "NT AUTHORITY\IUSR:(OI)(CI)(RX)"
icacls . /grant "Builtin\IIS_IUSRS:(OI)(CI)(RX)"

Or you can add permission using the GUI
Search user "IUSR" and "IIS_IUSRS" and grant the permission

IIS Setting

When wfastcgi enabled, you should able to find the "FastCGI Settings" in your IIS root
image
In your Site Application, open "Handler Mappings" page and select "Add Module Mapping..." on Actions

Request path:
*
Module:
FastCgiModule
Executable (optional):
[YOUR_PYTHON_EXE_PATH]|[YOUR_WFASTCGI_EXE_PATH]
Name:
FlaskHandler

image

Click the "Request Restrictions..." button
Uncheck the "Invoke handler only if request is mapped to:" checkbox

image
Back to the IIS root FastCGI Settings, and you should able to see record which "Full Path" and "Arguments" defined with your Python and wfastcgi path
Double click to Edit the record
Edit the Environment Variables under FastCGI Properties

Create 2 new members
Name: PYTHONPATH
Value: [YOUR_APPLICATION_ROOT_FOLDER]

Name: WSGI_HANDLER
Value: [YOUR_ENTRY_PY_FILE_NAME].app

image
image
image

References

Deploy a Python Flask Application in IIS Server and run on machine IP address
IIS部署python Web 使用Flask
如何將 Django 架設在 IIS 上

⚠️ **GitHub.com Fallback** ⚠️