locked
Configuring Python on IIS 7 RRS feed

  • Question

  • User-1705433311 posted

    Hi !

    How do I configure Python to execute CGI's/Py scripts? I'm at a loss in the new IIS manager - and don't even see the CGI-module. I reviewed the KB-article on using Python with IIS 5.0 (http://support.microsoft.com/kb/276494) but can not find equivalent options in IIS 7.

     Any pointers appreciated.

    Thanks!

    Saturday, June 16, 2007 7:04 PM

Answers

  • User113421904 posted

    Hi,

    You can add new Python CGI Script Map in IIS 7 management UI from:

    "Features View" -> "Handler Mappings"

     

    Here are the steps how I make Python running in IIS 7. Hope it helps:

    1. Please make sure Python is installed properly.

    2. Make sure CGI module is installed in IIS 7 

    Control Panel -> Programs -> Program and Features -> Turn Windows features on and off -> Internet Information Services -> World Wide Web Services -> Application Development Features -> CGI module.

    3. Add web application for Python, In IIS Manager, right click Default Web Site -> Add Application, setting Alias e.g.: PythonApp, and make it pointing to some folder like C:\PythonApp, then click OK

    4. In Features View, open Handler Mappings, right click to Add Script Map ...

    5. In Request path, put "*.py" as the script files extension, In Executable select "C:\Python25\Python.exe %s %s", here is my Python installation path and its parameters, this is mentioned in the KB article, you can check what are these two parameters used for. Then giving the script mapping an appropriate Name, like Python. Click OK.

    6. Create or copy a test.py into the virtual directory (C:\PythonApp), you can find it in the KB article.

    7. In Browse View, right click browse to verify it.

     

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, June 19, 2007 5:29 AM

All replies

  • User113421904 posted

    Hi,

    You can add new Python CGI Script Map in IIS 7 management UI from:

    "Features View" -> "Handler Mappings"

     

    Here are the steps how I make Python running in IIS 7. Hope it helps:

    1. Please make sure Python is installed properly.

    2. Make sure CGI module is installed in IIS 7 

    Control Panel -> Programs -> Program and Features -> Turn Windows features on and off -> Internet Information Services -> World Wide Web Services -> Application Development Features -> CGI module.

    3. Add web application for Python, In IIS Manager, right click Default Web Site -> Add Application, setting Alias e.g.: PythonApp, and make it pointing to some folder like C:\PythonApp, then click OK

    4. In Features View, open Handler Mappings, right click to Add Script Map ...

    5. In Request path, put "*.py" as the script files extension, In Executable select "C:\Python25\Python.exe %s %s", here is my Python installation path and its parameters, this is mentioned in the KB article, you can check what are these two parameters used for. Then giving the script mapping an appropriate Name, like Python. Click OK.

    6. Create or copy a test.py into the virtual directory (C:\PythonApp), you can find it in the KB article.

    7. In Browse View, right click browse to verify it.

     

    • Marked as answer by Anonymous Tuesday, September 28, 2021 12:00 AM
    Tuesday, June 19, 2007 5:29 AM
  • User1646422947 posted

    Thank you for your reply...

    Following this setup, when I try a simple test file "test.py" I get the error below (note that the test file works under IIS 5.1 on my previous hardware, so I am comfortable that is OK..)

    Python 3.0.1 and IIS 7 under Windows Server '08 by the way...

    Thanks deeply, in advance, for any pointers...

    Regards,

    GREG...

    -------------

    HTTP Error 502.2 - Bad Gateway
    The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "".

    Module CgiModule
    Notification ExecuteRequestHandler
    Handler Python
    Error Code 0x00000000

    Friday, April 10, 2009 12:12 AM
  • User-1733199854 posted

     I also followed the instructions and having the same error:

    HTTP Error 502.2 - Bad Gateway

    The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are "".

     

    If I changed the test to test.php and test.html and then it worked ok. So, it is not the test contents.

    I already spend few hours on it without success... Any help would be appreciated!

    Saturday, May 9, 2009 10:57 PM
  • User1868523329 posted

    Hey there,

     Basically IIS works now and has received a request from the CGI module but of course the response was not valid. 

    Try this in your *.py file:

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-

    # enable debugging
    import cgitb
    cgitb.enable()

    print "Content-Type: text/plain;charset=utf-8"
    print

    print "Hello World!"
    You can get this information and more here there is also information on how to use FastCGI, and I recommend you check out Django as well and here is a online book to get you going. 
     

     

    Thursday, September 10, 2009 12:25 PM
  • User1585693889 posted

     Hello,

     I tried to add Python Script Map in IIS7 in Windows 7, but failed. I followed those major procedures:

    1) I added Script Map for the default web site:

     

    Then you can see it has been added to this web site:

     

    2)  I created a a test.py into C:\inetpub\wwwroot

     from Http import *

    def Request():
    Header("Content-type: text/html")
    Write("Hello, World!")

    3) I tested http://localhost/test.py

     

     I followed the instruction step by step. I do not know what the problem is.

    Thanks a lot for any help !!

     

    Friday, June 25, 2010 8:14 PM
  • User1967057346 posted

    Hello Cecily,

    I have exactly the same problem. I try to make Mercurial work on Windows 7. For that I need to make Python 3.1 works on IIS7. After following the instructions of here and  here  I get the same error as you did! I wonder if you could solve the problem till now, probably you did:) can you or any  one else tell me what I do wrong?

    Thanks a lot in advance!! 

     

     

    Tuesday, October 19, 2010 2:36 AM
  • User-582375656 posted
    HTTP Error 502.2 - Bad Gateway
    The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are ""
    

    I was getting that error until I updated the mapping. initially i had just the python executable in the executable field, in fact you need an additional parameter:

    You're mapping should look like

    • Request Path: *.py
    • Executable: C:\Python27\python.exe -u "%s"
    • Name: python

    Then my script worked:


    print "Status: 200 OK"
    print "Content-Type: text/plain;charset=utf-8"
    print
    
    print "Hello World!"
    
    Wednesday, December 22, 2010 3:41 PM
  • User1539268832 posted
    Thanks to everyone contributing. The solution that I found is that the first line of the test script must print an empty line. HTML, HEAD and TITLE tags are not necessary. The explanation in this Tutorial, which also has a working test script.
    Tuesday, September 13, 2011 6:04 AM
  • User277293199 posted

    I had the same issue, please verify that the web.config should be something like this... I had some extra character in path value.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <handlers>
           <add name="cgi-bin" 
             path="*.py" 
             verb="*" 
             modules="CgiModule" 
             scriptProcessor="J:\Python27\python.exe %s %s" 
             resourceType="Unspecified" />
        </handlers>
      </system.webServer>
    </configuration>
    Thursday, October 31, 2013 1:59 PM