简体   繁体   中英

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?

One newline between each header. Two newlines between the headers and the body. And watch your line endings.

print 'Content-Type:image/png'
print 'Content-Disposition:attachment;filename=logo.png'
print

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM