Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Background:

I've attempted (strong emphasis on "attempt") to set up an application via IIS (6.1, Windows Server 2008 R1) that routes all requests through a python script. The end goal was to create a very light-weight API on the server.

Basically, I took the following steps:

  1. Added CGI feature to windows
  2. Created a new Application under the Default Web Site
  3. Added a handler mapping that accepts * and runs c:\Python27\python.exe -u "c:\inetpub\wwwroot\testapi\cgiadapter.py"
  4. Set the virtual directory of the application to testapi
  5. Set the physical path of the application to c:\inetpub\wwwroot\testapi

To do a basic test, the following script was tested and works:

import cgi, cgitb

# Detailed error logging to screen
cgitb.enable()

# Output basic response
print 'content-type:text/plain'
print
print 'Hello World'

Then, I try to update the script to read in the request from stdin

import cgi, cgitb

# Detailed error logging to screen
cgitb.enable()

# Attempt to read the raw request from CGI
import sys
request = sys.stdin.read()

# Output basic response
print 'content-type:text/plain'
print
print 'Hello World'

Suddenly, I receive the following error: <type 'exceptions.IOError'>: [Errno 9] Bad file descriptor on the line containing stdin.read().

Question:

Why is stdin considered a bad file?

Have I configured something incorrectly in IIS or is this a limitation to IIS? Or, perhaps, is the fact that the request is delivered via stdin an incorrect assumption?

Thanks!

share|improve this question
add comment

2 Answers

I had the same problem and discovered that under IIS, you need to use the CONTENT_LENGTH environment variable to bound the amount of data you read from stdin. Here is what I added to my code to make it work on IIS7:

try:
    import os
    request = sys.stdin.read(int(os.environ['CONTENT_LENGTH']))
except:
    request = ''
share|improve this answer
    
I, unfortunately, no longer have access to the environment that I originally tested this in, but this definitely would have been an interesting thing to try (and something that I hadn't looked into the first time through) –  zashu Feb 23 '13 at 23:05
add comment

A little bit late... but I think that stdin (fd 0) was closed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.