Running a python web app (flask) Windows Server (IIS) using HttpPlatformHandler or fastcgi and wfastcgi.py
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.
- Announcing: Running Ruby on Rails on IIS8 (or anything else, really) with the new HttpPlatformHandler (www.hanselman.com)
- Announcing the Release of the HttpPlatformHandler Module for IIS 8+ (azure.microsoft.com)
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:
- grab wfastcgi.py
- copy it to c:\inetpub\wwwroot
- run the .bat file which executes five appcmd.exe that modify applicationHost.config for you.
- 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:
- Configuring Python with Windows Azure Web Sites (windowsazure.com)
- Django Hello World Web Application (windowsazure.com)
- One of Microsoft’s Best-Kept Secrets - Python Tools for Visual Studio (PTVS) (hanselman.com)