title: Flask, wfastcgi, and IIS on Windows Server 2012
body: <p>I am trying to deploy a flask service on IIS on Windows Server 2012. To arrive at this point:</p>
  
  <ol>
  <li>pip installed flask (Python versions 2.7 and 3 were already installed.)</li>
  <li>pip installed wfastcgi</li>
  <li>Ran wfastcgi-enable</li>
  <li>Established a new site under IIS</li>
  <li>Added a handler for wfastcgi </li>
  <li>Modified Web.config</li>
  </ol>
  
  <p>Running from localhost returns the output I expect. However, when I visit the website from the sitename, the following error is returned (paths omitted):</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <p>I have included <code>app.py</code> and <code>Web.config</code> below. I omitted the <code>scriptProcessor</code> path here, but it is set to the value returned from wfastcgi-enable.</p>
  
  <p>When running from localhost, the environment is available. <strong>How do I make the environment available to the app when called beyond locahost?</strong></p>
  
  <p><strong><code>app.py</code></strong></p>
  
  <pre><code>from flask import Flask  
  myapp = Flask(__name__)
  
  @myapp.route("/hello") 
  def hello():
    return "Hello from flask!"
  
  if __name__ == "__main__":        
    myapp.run(port=8080)
  </code></pre>
  
  <p><strong><code>Web.config</code></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;configuration&gt;
      &lt;appSettings&gt;
          &lt;add key="WSGI_HANDLER" value="app.myapp" /&gt;
          &lt;add key="PYTHONPATH" value="c:/inetpub/wwwroot/flask-services/" /&gt;
          &lt;add key="WSGI_LOG" value="C:/TMP/logs/app.log" /&gt;
      &lt;/appSettings&gt;
      &lt;system.webServer&gt;
          &lt;handlers&gt;
              &lt;add name="python-wfastcgi" path="*" verb="*" modules="FastCgiModule" scriptProcessor="[Omitted]" resourceType="Unspecified" requireAccess="Script" /&gt;
          &lt;/handlers&gt;
      &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>