Search code examples
pythonwindowsapachecgimime

how to show mime data using python cgi in windows+apache


I met a problem while using python(2.6) cgi to show a mime data in windows(apache). For example, to show a image, here is my code: image.py

#!E:/program files/Python26/python.exe
# -*- coding: UTF-8 -*-
data = open('logo.png','rb').read()
print 'Content-Type:image/png;Content-Disposition:attachment;filename=logo.png\n'
print data

But it dose not work in windows(xp or 7)+apache or IIS. (I try to write these code in diferent way, and also try other file format, jpg and rar, but no correct output, the output data seems to be disorder in the begining lines.)

And I test these code in linux+apache, and it is Ok!

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
data = open('logo.png','rb').read()
print 'Content-Type:image/png;Content-Disposition:attachment;filename=logo.png\n'
print data

I just feel confused why it does not work in windows. Could anybody give me some help and advice?


Solution

  • Now I know how to solve this problem:

    • For windows+IIS: While adding the application mapping(IIS), write C:\Python20\python.exe -u %s %s. I used to write like this c:\Python26\python.exe %s %s, that will create wrong mime data. And "-u" means unbuffered binary stdout and stderr.
    • For windows+Apache: Add #!E:/program files/Python26/python.exe -u to the first line of the python script.

    Thank Ignacio Vazquez-Abrams all the same!