简体   繁体   English

如何在windows + apache中使用python cgi显示mime数据

[英]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). 我在使用python(2.6)cgi在Windows(apache)中显示mime数据时遇到问题。 For example, to show a image, here is my code: image.py 例如,要显示图像,这是我的代码: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. 但它不适用于Windows(xp或7)+ apache或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.) (我尝试以不同的方式编写这些代码,并尝试其他文件格式,jpg和rar,但没有正确的输出,输出数据似乎在开始行中无序。)

And I test these code in linux+apache, and it is Ok! 我在linux + apache中测试了这些代码,没关系!

#!/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. 我只是感到困惑,为什么它不能在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 . 对于Windows + IIS:在添加应用程序映射(IIS)时,编写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. 我曾经这样写c:\\Python26\\python.exe %s %s ,它将创建错误的mime数据。 And "-u" means unbuffered binary stdout and stderr. 而“-u”表示无缓冲的二进制stdout和stderr。
  • For windows+Apache: Add #!E:/program files/Python26/python.exe -u to the first line of the python script. 对于windows + Apache:将#!E:/program files/Python26/python.exe -u到python脚本的第一行。

Thank Ignacio Vazquez-Abrams all the same! 感谢Ignacio Vazquez-Abrams一样!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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