0

We followed this guide on setting up Flask on IIS https://medium.com/@dpralay07/deploy-a-python-flask-application-in-iis-server-and-run-on-machine-ip-address-ddb81df8edf3 and it works well but when we do flask run from VS code we get the following

PS J:\Apps\prod> flask run
 * Serving Flask app 'wsgi.py' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

About the development server line and the production WSGI server line - is this purely because we set it up in IIS? Should we have followed a different route than what was in that guide we followed?

Everything else I'm seeing seems to be only relevant to *nix based systems i.e using Gunicorn

Thanks

pee2pee
  • 3,619
  • 7
  • 52
  • 133
  • 1
    try this `export FLASK_ENV=production` to set environment variable to production mode and `flask run` – ShivaGaire Jul 06 '22 at 09:05
  • 1
    You have to follow official materials like https://learn.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2022#configure-the-httpplatform-handler, not any other post from the internet, and note that "We recommend using HttpPlatform to configure your apps, as the WFastCGI project is no longer maintained." – Lex Li Jul 06 '22 at 13:18
  • Thank you but the thing is, that doesn't work - https://serverfault.com/questions/1104949/python-flask-on-iis-httpplatform-doesnt-load-when-visiting-hostname – pee2pee Jul 06 '22 at 16:41

3 Answers3

3

The guide you followed, though captured quite a few screen shots and contained a lot of information, does not describe the recommended approach from Microsoft, because Python via FastCGI on IIS is no longer supported as quoted from here,

We recommend using HttpPlatform to configure your apps, as the WFastCGI project is no longer maintained.

To get up-to-date information on this setup, you can refer to my blog post. I list the key points below to make a standalone answer.

Preparation

Prepare a folder such as C:\flask-test, and create app.py as

from flask import Flask

def create_app():
    app = Flask(__name__)

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

    return app

and wsgi.py

from app import create_app

application = create_app()

Then install both flask and waitress,

pip install flask waitress

And test that both flask run and waitress-serve work,

PS C:\flask-test> ~\AppData\Local\Programs\Python\Python310\python.exe -m flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)

PS C:\flask-test> ~\AppData\Local\Programs\Python\Python310\python.exe -m waitress --port 9000 wsgi:application
INFO:waitress:Serving on http://0.0.0.0:9000

IIS Integration

Add a web.config file in this folder to enable HttpPlatformHandler,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
        <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\python.log" startupTimeLimit="20" processPath="C:\Users\lextudio\AppData\Local\Programs\Python\Python310\python.exe" arguments="-m waitress --port %HTTP_PLATFORM_PORT% wsgi:application">
        </httpPlatform>
    </system.webServer>
</configuration>

Then on IIS you can create a new site to point to C:\flask-test.

More details can be found in my blog post, especially on troubleshooting.

Lex Li
  • 60,503
  • 9
  • 116
  • 147
0

Prepare a folder such as C:\flask-test, and create app.py as

from flask import Flask
from gevent.pywsgi import WSGIServer

app = Flask(__name__)

@app.route('/api', methods=['GET'])
def index():
    return "Hello, World!"

if __name__ == '__main__':
    # Debug/Development
    # app.run(debug=True, host="0.0.0.0", port="5000")
    # Production
    http_server = WSGIServer(('', 5000), app)
    http_server.serve_forever()

Note: Install gevent using pip install gevent

Abolfazl Rastgou
  • 655
  • 10
  • 13
0

You can use waitress. It's a production WSGI server.

from waitress import serve
serve(app, host="0.0.0.0", port=8080)

It's a drop-in replacement to your classic app.run().