http://netdot.co/2015/03/09/flask-on-iis/
While Windows Server is usually not my preferred OS for deploying Python based web[sites|services] sometimes the situation dictates it. For situations where that is the case I’ve usually reached for Apache with mod_python until now. FastCGI is relatively straightfoward to setup on Windows Server 2012R2 and IIS using the Microsoft Web Platform Installer.
(NOTE: I’m using Flask, but any WSGI compatible Python app will work the same way)
Installing IIS
You’ll need to make sure that IIS is installed and configured with the CGI role service (this enables FastCGI as well).
Go to Server manager -> Manage -> Add Roles and Features:
Choose Role-based or feature-based installation:
Select your server:
Choose “Web Server (IIS)”:
Click “Add Features”:
Accept the defaults until you reach the “Role Services” screen. At this point make sure “CGI” is selected:
Click next, accept defaults and click “Install”. You should see this screen:
Launch the IIS Manager:
If this message pops up, click “Yes” - otherwise you’ll need to download and install the Web Platform Installer if you haven’t already installed it:
Run the installer
Search for WFastCGI
Select the appropriate Python version (3.4 or 2.7.9). We’ll choose 3.4. Click “Add”, then click “Install”.
Read and accept the license (if you want to continue):
Once you’re done - you should see this screen:
Setup your site - copy the wfastcgi.py from C:\Python34 (may be named C:\Python34_x86 if you had an existing Python34 directory) to your Flask application root:
Double click “Handler Mappings”
Click “Add Module Mapping”
Click “Request Restrictions”. Make sure “Invoke handler only if request is mapped to:” checkbox is unchecked:
Click “Yes” here:
Go to the root server settings and click “FastCGI Settings”:
Double click it, then click the “…” for the Environment Variables collection to launch the EnvironmentVariables Collection Editor:
Set the PYTHONPATH variable:
And the WSGI_HANDLER (my Flask app is named app.py so the value is app.app - if yours is named site.py it would be site.app or similar):
Click OK and browse to your site:
Flask code below:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
app.run()