Python has proved itself to be an extremely useful and multi-faceted tool. We’ve used it with libraries such as pandas and numpy in the Data Science arena, working with client data to gain insight into their business. In a different field completely, on occasions it has also been tremendously convenient for building web sites.

The excellent Flask library makes it an absolute breeze to put together a simple, interactive website, capable of running on any mainstream platform. We needed one particular such website to be served using Microsoft IIS, as it was working alongside another ASP.Net site. I had feared that making this happen was going to be difficult, but in the event it was extremely straightforward. I did have to piece together information from a few different sources, though, so thought I’d document the process for next time, going right from scratch.

For the purposes of this note, we are going to work with the very simple “Hello, World” application in the Flask documentation. Then we are going to use the wfastcgi package to serve the site in IIS. Steps to get this working using the in-built standalone server on Windows are as follows:

Set up python

  • Install Continuum’s excellent miniconda to C:\miniconda (I used the 64-bit Python 2.7)
  • Set up some python packages we’ll be using. From a command prompt, run:
    conda install flask pip
    pip install wfastcgi

Create the python application

  • Create a directory C:\pyproject
  • In that directory, create a file called server.py, containing the following:
    from flask import Flask
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return 'Hello World!'
    
    if __name__ == '__main__':
        app.run()
  • Check the site works: from a command prompt, go to that directory and type:
    python server.py
  • From a web browser on the same computer, attempt to view the web page http://localhost:5000; the application should introduce itself!

Get it running in IIS

First, we need to ensure that the computer has a few essential options configured. Go into “Server Manager”, then:

  • Make sure the “Web Server (IIS)” role is set up
  • Right-click on that role on the left panel, choose “Add Role Services”, then ensure that CGI (under Application Development) is installed
  • Now click on “Internet Information Services (IIS) Manager” on the left and drill in on the right so that you can right-click on the “Sites” element and choose “Add Web Site”
  • Add a site called “pyproject”, with a physical path of C:\pyproject, and bind it to port 81 (I’m assuming you already have a default site using port 80, and you can change this later if required)
  • Click on your new web site, and double-click on “Handler Mappings” on the right pane under IIS
  • Pick “Add Module Mapping” on the right, and enter:
    • Request path: *
    • Module: FastCgiModule
    • Executable: C:\Miniconda\python.exe|C:\Miniconda\lib\site-packages\wfastcgi.pyc
    • Name: python-wfastcgi
  • When you continue, you will be prompted to create a FastCGI application; say “Yes”
  • Click on your new website again, and go into “Application Settings” under ASP.Net, then add:
    • PYTHONPATH as C:\pyproject
    • WSGI_HANDLER as server.app.wsgi_app

Check it works

Open a web browser, and this time navigate to http://localhost:81. The web page should be the same as the test above, but now the site is being served by IIS.

Results

You should now have a fully-functioning version of your web site. The standard options are then available for managing the application, and everything should operate correctly if you change the site code. There are further options you can use to improve logging, which are documented on the wfastcgi page, but hopefully this should be enough for you to get started with python web sites on Windows.