0

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!

zashu
  • 143
  • 6

2 Answers2

1

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 = ''
Beevik
  • 111
  • 2
  • 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
0

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

estani
  • 2,151
  • 2
  • 17
  • 13