Deprecated: Assigning the return value of new by reference is deprecated in /fs5e/blendedtechnologies/public/wp-content/plugins/pmetrics.php on line 1192
Blended Technologies » Blog Archive » Setting Up IIS to Use Python

Setting Up IIS to Use Python

Wow, this should have been easy, but it just wouldn’t work:

These websites will get you started and help you get everything set up:
MSDN: Using Python Scripts with IIS
Let’s Configure Windows 2000 IIS with CGI Scripts

Now I will provide my troubleshooting experiences from this morning:
—Trouble Shooting—

Problem 1: Not Authorized to view your test script
Error was:

You are not authorized to view this page
You do not have permission to view this directory or page using the credentials
you supplied.
Please try the following:

    * Click the Refresh button to try again with different credentials.
    * If you believe you should be able to view this directory or page,
       please contact the Web site administrator by using the e-mail address
       or phone number listed on the localhost home page.

HTTP 401.3 - Access denied by ACL on resource
Internet Information Services

Technical Information (for support personnel)

    * More information:
      Microsoft Support

Solution: Right click on your test script file (or the entire directory), select properties, select the security tab, make sure that IUSR_[your computer name] and IWAM_[your computer name] has read and execute permissions. If they are not listed, click add. If you can’t find them to add, click advanced and make sure you search the entire computer (todo: add picture)

Problem 2: Python doesn’t see my test file.
error was:

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

C:Python24python.exe: can't open file 'C:Documents': [Errno 2] No such file
or directory

Solution: Fix your application mapping:
To do this, perform the following steps:
a. In the ISM, under Internet Information Server, right-click your website and select properties
c. Click the Home Directory or Virtual Directory tab (which ever you see), and then click Configuration.
d. Edit your mapping for the .py (other other Python extension) Executable to be:
C:\Python24\python.exe -u “%s” “%s”
(use your python path of course, keep those quote marks, they’re vital!)

Problem 3: My browser keeps asking me if I want to save the test script!
My browser prompts me with:

You have chosen to open
which is a: Python File
from: http://localhost
What should Firefox do with this file?

Solution: Script needs to print correct content type.
The test script from the MSDN link above tells you to start with this:

print 'Status: 200 OK'
print 'Content-type: text/html'

That did not work for me. Once I changed my test script to start with:

print "Content-Type: text/html\n\n"

everything was good.

Please post any questions in the comments section. I hope my lost morning helps someone out there!

addition: this website looks useful for setting up Python for cgi on Windows Server 2003 |  Digg |  FURL |  Yahoo! My Web 2.0 |  Reddit

11 Responses to “Setting Up IIS to Use Python”

  1. beeduul Says:

    thanks — #3 now works for me! *whistle*

    that microsoft site is drain bamaged, grr.

  2. Mike Schinkel Says:

    Hi. Can I assume you set this up on IIS 6.0/Windows 2003 Server? Did you use ActiveState, or the standard Python 2.4 distribution? Also, do you have sample scripts? Does they look like ASP/VBScript only with Python syntax, or do they look like something else? Thanks in advance (and if your website doesn’t email notices that you’ve updated, I’d love and email note telling me you’ve commented on this.)

  3. Greg - CEO/Founder Says:

    Mike, I’ve used this same technique on both Windows 2003 Server and Windows XP. I forget which version of IIS though.

    These are just standard Python files, nothing more. The setup in this post just tells IIS to ask the standard Python interpreter to run your files.

    You can go the ASP with Python syntax route, but that’s not covered here, you’d have to look that up how to do that, but I know it’s possible.

    Just let me know if you need anymore help.

  4. mike Says:

    Dang it’s a shame I didn’t find this site last week when I was banging my head on these same issues. It’s all working pretty good now though.
    I can use Python in .ASP pages but it doesn’t seem possible to use this like Python Server Pages and other dynamic template engines.

    I’m interested in trying Cheetah now.

  5. Greg - CEO/Founder Says:

    Hi Mike,

    How are you using Python ins .ASP pages? That sounds promising. My biggest complaint with using Python in IIS is that the pages seem to be slow to load. Maybe Python in ASP would fix that?

  6. Joe Smith Says:

    We have just read your article.
    Now we have to set up the python CGI development environment on XP-SP2.
    We have done what the following articles tell us, but the famous “CGI Error The specified CGI application misbehaved by not returning a complete set of HTTP headers” is not fixed:
    Could you please give us some advice?
    We will wait for your reply. Thank you.

  7. Greg - CEO/Founder Says:


    Offhand, my first advice is to look again at the problem 2 section of my article above. I think the problem lies in that area. Did you put in the correct Python path? Also make sure you’re using regular double quotes (it looks like my article here changed them to fancy quotes which probably wouldn’t work.)

    Let me know if that helps.

  8. PS Says:

    Hi there- you might want to look into using an ISAPI server extension. With all the work you’ve put into getting this going, you might as well take the extra step for the performance gain. It keeps the Python interpreter loaded in between requests, and from the looks of it, CGI has been a pain for you anyway.

    You won’t get templating per se, but adding a framework like Django in the mix will give you a pretty nice and fast template system.

  9. Greg - CEO/Founder Says:

    PS, that looks very promising. How does it compare to this FastCGI extension for IIS:

  10. Anon Says:

    I was going crazy because I was following the directions in the Microsoft KB but it was not working! Changing the print statement as specified in you problem 3 solutions was the answer. Thank you thank you thank you!

  11. Anon Says:

    p.s., I was wondering why Microsoft would put out a kb that simply did not work. Out of curiosity I decided to try the Microsoft code with the header as described in the kb in Internet Explorer:
    print ‘Status: 200 OK’
    print ‘Content-type: text/html’

    Interestingly, the microsoft code works in IE7 but not in FF2. Any ideas on why this would be?