Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask, wfastcgi, and IIS on Windows Server 2012

I am trying to deploy a flask service on IIS on Windows Server 2012. To arrive at this point:

  1. pip installed flask (Python versions 2.7 and 3 were already installed.)
  2. pip installed wfastcgi
  3. Ran wfastcgi-enable
  4. Established a new site under IIS
  5. Added a handler for wfastcgi
  6. Modified Web.config

Running from localhost returns the output I expect. However, when I visit the website from the sitename, the following error is returned (paths omitted):

Error occurred while reading WSGI handler:

Traceback (most recent call last):
  File "wfastcgi.py", line 791, in main
    env, handler = read_wsgi_handler(response.physical_path)
  File "wfastcgi.py", line 633, in read_wsgi_handler
    handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
  File "wfastcgi.py", line 586, in get_wsgi_handler
    raise Exception('WSGI_HANDLER env var must be set')
Exception: WSGI_HANDLER env var must be set

This is the case whether on the server or from another machine on the domain. It seems as though when the app is requested from anything but localhost, the environment is unreachable. Nothing gets written to the wfastcgi log.

I have included app.py and Web.config below. I omitted the scriptProcessor path here, but it is set to the value returned from wfastcgi-enable.

When running from localhost, the environment is available. How do I make the environment available to the app when called beyond locahost?

app.py

from flask import Flask  
myapp = Flask(__name__)

@myapp.route("/hello") 
def hello():
  return "Hello from flask!"

if __name__ == "__main__":        
  myapp.run(port=8080)

Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appSettings>
        <add key="WSGI_HANDLER" value="app.myapp" />
        <add key="PYTHONPATH" value="c:/inetpub/wwwroot/flask-services/" />
        <add key="WSGI_LOG" value="C:/TMP/logs/app.log" />
    </appSettings>
    <system.webServer>
        <handlers>
            <add name="python-wfastcgi" path="*" verb="*" modules="FastCgiModule" scriptProcessor="[Omitted]" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
    </system.webServer>
</configuration>
like image 551
twip Avatar asked Dec 14 '25 10:12

twip


1 Answers

We had a similar problem recently with IIS 7, Flask 0.12 and Python 3.6.4. Your web.config looks good. Two recommendations:

  1. Use a virtualenv for your script processor. It avoids side-effects from using a system-level Python installation. It's easier to debug that way.
  2. Double check in IIS that IIS_IUSRS and IUSR have modify permissions for the folders in your Python path.
like image 110
susodapop Avatar answered Dec 16 '25 00:12

susodapop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!