Tuesday, February 1, 2011

Setting up Python on IIS 7

(This instruction is based the post here, with added details).

  1. Make sure Python is installed properly (I’m using Python 2.7.1 in this example).
  2. If you don’t have IIS CGI module installed, you can go to Control Panel->Programs and Features, and then click on “Turn Windows features on or off”. Then, in “Windows Features” window, make sure Internet Information Service->World Wide Web Services->Application Development Features->CGI is checked (IMG 1). Click “OK” to apply changes.
  3. Launch IIS Manager (Control Panel->Administrative Tools->Internet Information Services (IIS) Manager).
  4. Right click on “Default Web Site” and select “Add Application…”.
  5. Enter an application Alias, and specify a local path where you’ll put your Python scripts (IMG 2). Click “OK”.
  6. With the new application node selected, double-click on “Handler Mappings” in the middle panel. Then, click “Add Script Map…” link in “Actions” box to the right.
  7. In “Add Script Map” window, enter “*.py” as Request Path, and your python.exe as Executable. Note two parameters (-u %s) are added to the command line. Give the mapping a name (such as “Python”) and then click “OK”.
  8. Create a new Python script in your script folder in step 5. Note that you’ll need to return complete HTTP header for your script to work:
    print "Status: 200 OK"
    print "Content-Type: text/plain;charset=utf-8"
    print
    print "Hello Python!"

  9. Launch browse and navigate to the script (http://localhost/PythonApplication/HelloPython.py) to see the result.

image

IMG 1: IIS CGI Module

image

IMG 2: Application settings.

image

IMG 3: Python mapping.

0 comments:

Post a Comment