0

I'm trying to return a png image to browser using python cgi on IIS.

I got the same problem with this: Return an image to the browser in python, cgi-bin

but the solutions provided there don't work on my machine.

Here is a brief intro to my situation:

  1. I have a png image on C:\logs\demo.png, this image is absolutely fine and can be opened by a visual image viewer;

  2. I have a python script with the code below:

print 'HTTP/1.1 200 OK'
print 'Content-Type:image/png\n'
print file('c:\\logs\\demo.png', 'rb').read()

3 . when I visit the corresponding url, nothing returned, and firefox says the image contains errors. The length returned in the response is also very small.

Is there any idea about the problem? I'm getting crazy~

Any reply is appreciated:)

Community
  • 1
  • 1
taijirobot
  • 143
  • 1
  • 2
  • 8
  • print doesn't preserve things exactly - for starters, it'd be adding a newline after the file data. I'd use sys.stdout.write() to make sure you're writing exactly what you're meaning to. EDIT: Also, how far off is the response size - how large is the file & what's the reported response size? Do firefox and IIS agree on that? – AdamKG Jan 12 '12 at 21:27
  • sys.stdout.write() doesn't work either, the same as print here. The picture is about 40KB, but the response is only about 1KB. – taijirobot Jan 12 '12 at 22:41
  • and a strange thing is that in another script, I used sys.stdout.write, this write returns 40KB content, but still no picture shows. I've double checked the two newlines between header and content. – taijirobot Jan 12 '12 at 22:52

1 Answers1

1

Problem solved.

Finally I found an error in IIS configuration.

The extension mapping should be:

c:\Python27\python.exe -u "%s" "%s"

-u prevents newlines from being converted to Carriage return/ newline combinations which can sometimes cause problems

The article below might be useful for configuration. Very detailed and clear. http://python.net4geeks.com/iiswse.asp

taijirobot
  • 143
  • 1
  • 2
  • 8