Elvenware Logo


Python on Windows


CGI and Python in IIS

This text accompanies a video stored on the YouTube site.. To install or get started with Python, see the Python Install section.

Open  the control panel: Control Panel | Programs | Turn Windows features on or off

Download Python here. More on Python here. Examples of calling a Python Script here.

Select and turn on  Internet Information Services | World Wide Web Services | Application Development Features | CGI.

Adding CGI{height="489"}

It is fine, or perhaps even recommended that, you also select ISAPI Extensions and ISAPI Filters.

Now you want to "add an application" to IIS, which really means you want to configure a directory on your web site. The first step is to launch the IIS manager. There are several ways to do this:

In IIS manager, select your web site. On the right, choose View Applications. Choose Add Application. Provide an Alias such as cgi-bin or PythonApps. When choosing the path, you may need to do a bit of thinking, depending on whether or not your development site has the same structure as your published site. For instance, the cgi-bin directory on my web server is not in the same folder level as the rest of my site.

Add an application

Figure 02: Configuring an Application in the IIS Manager

If you have configured everything correctly, then your new "application" will appear with a shiny blue globe icon in the Connections panel, as shown in Figure 3. Note that on the right there is an option to View Applications, click this link to see how the application is configured.

cgi\_bin and PythonApp are both configured for cgi

Figure 03: The folders cgi_bin and PythonApp are both configured for cgi. Click to zoom.

The next step is to set up the Script Map for Python. In the IIS Manager, choose the "Application (that is the directory, which in this case is called cgi_bin) that you set up and select Handler Mappings and then on the right choose Add Script Map. Set the Request Path to *.py and the Executable to the location of your Python installation: C:\Python27\python.exe %s %s. I wrote Python in the Name field.


Please note that if you accidentally leave off the %s %s part, then you will get a 502.2 error. See the HTTP status code page.

For more information, go here and here.

Where is CGI Handler Information Written?

Usually, when you create Handler Mappings for your files, they end up in a Web.config file in the directory that you set up for your cgi scripts. Typically the relevant entry in your Web.config file looks something like this:

<?xml version="1.0" encoding="UTF-8"?>
       <add name="cgi-bin"
         scriptProcessor="J:\Python27\python.exe %s %s"
         resourceType="Unspecified" />

I believe that this file alone would be enough to configure a directory to handle Python scripts. In other words, you don't need to use the GUI to create the file, it is simply a tool for creating the file.

I have, however, found a case on my system where there is no web.config file, and yet everything is working. It happens that this directory is not in the same folder as the rest of my web site. So if there is no Web.config file in the expected place, where is the information being stored?

After some mucking about, I went to this folder:


And in that directory there was a file called applicationhost.config. And in that file, there was the section where first my virtual directory was defined:

<application path="/cgi-bin" applicationPool="Elvenware">
<virtualDirectory path="/" physicalPath="J:\cgi-bin" /></application>

And then at the very end of the that lengthy file there was this section sets up Python scripts:

<location path="Elvenware/cgi-bin">
        scriptProcessor="J:\Python27\python.exe %s %s"
        resourceType="Unspecified" />

Clearly the data in this last section mirrors what was in the web.config file on my other systems. It was just a question of whether it was stored in a global configuration file or in a local web.config file.

You read in the official Microsoft document about applicationhost.config.

Various Error Codes

When you are working with HTML on IIS, or if you are working with Python Scripts on IIS, you are likely to encounter a number of hard to understand errors such as 404.2, 502.2, Errno 13, etc. I try to document some of them here:

Copyright © Charlie Calvert | Elvenware Home | Writing Code | Delphi | CSharp | My Books