Running a python web app (flask) Windows Server (IIS) using HttpPlatformHandler or fastcgi and wfastcgi.py

Aaron ❤️ ☁️
3 min readJun 30, 2013

Update (2015): This has recently become a lot easier on Windows if you are using IIS 8+ or Azure Web Apps. These days I would dispense with using wfastcgi.py, take my python flask app, host it in Tornado (~7 lines of code), and use Microsoft’s new HttpPlatformHandler extension to proxy requests to it. The process is very “batteries included”, you get a lot of functionality for free, and you can take nearly identical steps to host Go (golang), Java, Ruby or anything else behind IIS.

The content from the original post remains below.

this is one of my first technical posts on medium, so i’ll keep it short.

it has often been perceived that it is difficult to get python running in iis. however, we can take advantage of some work microsoft has done driving multi-language support on windows azure. wfastcgi.py is microsoft’s pure python wsgi adapter which lets use use python painlessly on windows server via fastcgi. fastcgi is the same solution that has been working with php for years (and is recommended by zend). here’s how to do it:

  1. grab wfastcgi.py
  2. copy it to c:\inetpub\wwwroot
  3. run the .bat file which executes five appcmd.exe that modify applicationHost.config for you.
  4. run flask, or any other python framework or micro-framework you please.

i’ve made a one line tweak to the latest wfastcgi.py that adds the DOCUMENT_ROOT to the PYTHONPATH so that it can find your main.py with ease.

you can grab the tweaked wfastcgi.py, .bat file and a flask sample here: https://gist.github.com/solevangelist/5845664

note: instead of having _appcmd.bat add the handler to all sites via the following lines (#1 and #2):

%windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers /accessPolicy:Read,Script

%windir%\system32\inetsrv\appcmd set config /section:system.webServer/handlers “/+[name=’Python_via_FastCGI’,path=’*’,verb=’*’,modules=’FastCgiModule’,scriptProcessor=’C:\Python27\python.exe|C:\inetpub\wwwroot\wfastcgi.py’,resourceType=’Unspecified’]”

you may omit this line and edit the applicationHost.config via the Internet Information Services (IIS) Manager itself via Site > Handler Mappings > Add Module Mapping…

it’s really nice running python apps inside iis, because one gets the advantage of being able to run .net (or other) apps on the same site, serve static files separately (without every request hitting python itself), output caching, logging, etc.

these are the two windows azure tutorials that helped me on my way. but they are a lot longer than this post.

See also:

--

--

Aaron ❤️ ☁️

Code, Clouds & Security. Senior Cloud Developer Advocate @Microsoft + @Azure (prev. $RAX). Also: humor, music, photography, finance, languages, learning, health